- Manager中进程数据不安全
- 通过加锁解决
from multiprocessing import Manager,Process,Lock
def main(dic,lock):
lock.acquire()
dic['count'] -=1
print(dic)
lock.release()
if __name__ == '__main__':
m = Manager()
l = Lock()
dic = m.dict({'count':100}) #主进程中数据提供到子进程去操作
p_lst = []
for i in range(50):
p = Process(target=main,args=(dic,l))
p.start()
p_lst.append(p)
for i in p_lst:i.join()
print('主进程',dic)