不同的进程不能同时修改一份数据,但是不同的进程能对一份数据进行修改
可通过Manager来实现进程间的数据共享
# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"
from multiprocessing import Process,Manager
import os
def Child_Process(a,b):
a[os.getpid()] = os.getpid()
b.append(os.getpid())
print(b)
if __name__ == '__main__':
d = Manager().dict() #生成一个字典,可在多个进程间传递和共享
l = Manager().list() #生成一个列表,可在多个进程间传递和共享
p_list = []
for i in range(10):
p = Process(target=Child_Process,args=(d,l))
p.start()
p_list.append(p)
for r in p_list:
r.join()
print(d)
print(l)
运行结果
父进程开启了10个子进程,10个子进程又把自己的进程号传入了数组和字典