Manager 能够实现进程之间的数据共享(dict list),但是必须上锁来确保数据的准确性,
队列则可以实现进程之间数据通信
from multiprocessing import Process , Manager ,Lock def work(d,lock): # 自动上锁和解锁 with lock: d["count"] -= 1 """ lock.acquire() d["count"] -= 1 lock.release() """ if __name__ == "__main__": lock = Lock() m = Manager() dic = m.dict({"count":50}) lst = [] for i in range(50): p = Process(target=work,args = (dic,lock)) lst.append(p) p.start() # 等待每一个进程执行完毕 for p in lst: p.join() print(dic)
执行结果:
{'count': 0}