进程之间数据共享 Manager()
进程之间数据共享
-
使用Manager()
-
缺点:
- 数据不安全(可以加锁解决)
- 速度慢
-
代码:
from multiprocessing import Process from multiprocessing import Manager from multiprocessing import Lock def change_dic(dic,lock): with lock: dic['count']-=1 if __name__ == '__main__': with Manager()as m: #m=Manager() lock = Lock() dic=m.dict({'count':100}) p_l=[] for i in range(100): p=Process(target=change_dic,args=(dic,lock)) p.start() p_l.append(p) for p in p_l:p.join() print(dic) 得: {'count': 0}