多进程共享数据,真正的通信Manager

时间:2024-07-29 22:07:44
Managers

A manager object returned by Manager() controls a server process which holds Python objects and allows other processes to manipulate them using proxies.

A manager returned by Manager() will support types listdictNamespaceLockRLockSemaphoreBoundedSemaphoreConditionEventBarrierQueueValue and Array. For example,

from multiprocessing import Process,Manager
import threading,time,os def sub(d,l):
d[os.getpid()] = os.getpid()
l.append(os.getppid())
print(l)
#print(d)
if __name__ == '__main__':
with Manager() as M: #with..as的作用是把Manager()生成一个别名M,并只有在with内有效
d = M.dict() #用了with,下面也可以不用。
l = Manager().list(range(5))
p_list = [] #这个列表保存生成的多个进程 ,方便促一join()。等待全部完成,
for i in range(10):
t = Process(target=sub,args=(d,l))
t.start()
p_list.append(t)
for i in p_list:
i.join()
print(d,l)

  ...