脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python实现多进程共享数据的方法分析

Python实现多进程共享数据的方法分析

2020-12-22 00:02HeatDeath Python

这篇文章主要介绍了Python实现多进程共享数据的方法,结合实例形式分析了Python多进程共享数据的相关实现技巧,需要的朋友可以参考下

本文实例讲述了Python实现多进程共享数据的方法。分享给大家供大家参考,具体如下:

示例一:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# -*- coding:utf-8 -*-
from multiprocessing import Process, Manager
import time
import random
def kkk(a_list, number):
  for i in range(10):
    a_list.append(i)
    time.sleep(random.randrange(2))
    print('这是进程{} {}'.format(number, a_list))
  print('这是进程{} {}'.format(number, a_list))
def jjj(a_list):
  for i in range(2):
    process = Process(target=kkk, args=(a_list, i))
    process.start()
if __name__ == '__main__':
  a_list = []
  process_0 = Process(target=jjj, args=(a_list,))
  process_0.start()
  process_0.join()
  print(a_list)
  print(len(a_list))
  print('it\'s ok')

输出:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
这是进程0 [0]
这是进程0 [0, 1]
这是进程0 [0, 1, 2]
这是进程0 [0, 1, 2, 3]
这是进程1 [0]
这是进程0 [0, 1, 2, 3, 4]
这是进程1 [0, 1]
这是进程0 [0, 1, 2, 3, 4, 5]
这是进程1 [0, 1, 2]
这是进程1 [0, 1, 2, 3]
这是进程1 [0, 1, 2, 3, 4]
这是进程1 [0, 1, 2, 3, 4, 5]
这是进程0 [0, 1, 2, 3, 4, 5, 6]
这是进程0 [0, 1, 2, 3, 4, 5, 6, 7]
这是进程0 [0, 1, 2, 3, 4, 5, 6, 7, 8]
这是进程1 [0, 1, 2, 3, 4, 5, 6]
这是进程0 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
这是进程0 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
这是进程1 [0, 1, 2, 3, 4, 5, 6, 7]
这是进程1 [0, 1, 2, 3, 4, 5, 6, 7, 8]
这是进程1 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
这是进程1 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[]
0
it's ok
Process finished with exit code 0

示例二:

使用 Manager

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# -*- coding:utf-8 -*-
from multiprocessing import Process, Manager
import time
import random
def kkk(a_list, number):
  for i in range(10):
    a_list.append(i)
    time.sleep(random.randrange(2))
    print('这是进程{} {}'.format(number, a_list))
  print('这是进程{} {}'.format(number, a_list))
def jjj(a_list):
  for i in range(2):
    process = Process(target=kkk, args=(a_list, i))
    process.start()
if __name__ == '__main__':
  manager = Manager()
  a_list = manager.list()
  # a_list = []
  process_0 = Process(target=jjj, args=(a_list,))
  process_0.start()
  process_0.join()
  print(a_list)
  print(len(a_list))
  print('it\'s ok')

输出:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
这是进程0 [0, 0]
这是进程0 [0, 0, 1]
这是进程0 [0, 0, 1, 2]
这是进程0 [0, 0, 1, 2, 3]
这是进程0 [0, 0, 1, 2, 3, 4]
这是进程1 [0, 0, 1, 2, 3, 4, 5]
这是进程0 [0, 0, 1, 2, 3, 4, 5, 1]
这是进程1 [0, 0, 1, 2, 3, 4, 5, 1, 6]
这是进程1 [0, 0, 1, 2, 3, 4, 5, 1, 6, 2]
这是进程1 [0, 0, 1, 2, 3, 4, 5, 1, 6, 2, 3]
这是进程1 [0, 0, 1, 2, 3, 4, 5, 1, 6, 2, 3, 4]
这是进程1 [0, 0, 1, 2, 3, 4, 5, 1, 6, 2, 3, 4, 5]
这是进程0 [0, 0, 1, 2, 3, 4, 5, 1, 6, 2, 3, 4, 5, 6]
这是进程0 [0, 0, 1, 2, 3, 4, 5, 1, 6, 2, 3, 4, 5, 6, 7]
这是进程0 [0, 0, 1, 2, 3, 4, 5, 1, 6, 2, 3, 4, 5, 6, 7, 8]
这是进程1 [0, 0, 1, 2, 3, 4, 5, 1, 6, 2, 3, 4, 5, 6, 7, 8, 9]
这是进程1 [0, 0, 1, 2, 3, 4, 5, 1, 6, 2, 3, 4, 5, 6, 7, 8, 9, 7]
这是进程0 [0, 0, 1, 2, 3, 4, 5, 1, 6, 2, 3, 4, 5, 6, 7, 8, 9, 7, 8]
这是进程0 [0, 0, 1, 2, 3, 4, 5, 1, 6, 2, 3, 4, 5, 6, 7, 8, 9, 7, 8]
这是进程1 [0, 0, 1, 2, 3, 4, 5, 1, 6, 2, 3, 4, 5, 6, 7, 8, 9, 7, 8]
这是进程1 [0, 0, 1, 2, 3, 4, 5, 1, 6, 2, 3, 4, 5, 6, 7, 8, 9, 7, 8, 9]
这是进程1 [0, 0, 1, 2, 3, 4, 5, 1, 6, 2, 3, 4, 5, 6, 7, 8, 9, 7, 8, 9]
[0, 0, 1, 2, 3, 4, 5, 1, 6, 2, 3, 4, 5, 6, 7, 8, 9, 7, 8, 9]
20
it's ok
Process finished with exit code 0

希望本文所述对大家Python程序设计有所帮助。

原文链接:http://blog.csdn.net/heatdeath/article/details/73824644

延伸 · 阅读

精彩推荐