多进程数据共享
from multiprocessing import Process,Manager #从多进程导入过程中,管理者
import os
def f(d,l,e):
d[os.getpid()]=os.getpid() #getpid得到各进程的id
l.append(os.getpid())
e.append(os.getpid())
print("l:",l)
print("d:",d)
print("e:",e)
if __name__=="__main__":
with Manager() as mager: #用于多进程间的共享变量
d=mager.dict()
l=mager.list()
p_list=[]
e=[] #未给e进行设置多进程间的共享
for i in range(3):
p=Process(target=f,args=(d,l,e))
p.start()
p_list.append(p)
for res in p_list:
res.join() #主进程等待子进程的完成后一起打印
运行结果如下:
l: [2164]
d: {2164: 2164}
e: [2164]
l: [2164, 22844]
d: {2164: 2164, 22844: 22844}
e: [22844]
l: [2164, 22844, 9244]
d: {2164: 2164, 22844: 22844, 9244: 9244}
e: [9244]
所以未给e进行设置多进程间的共享,即在执行过程中e只能得到当前进程的资源,不会共享到其他进程的资源