import multiprocessing
def worker(d, key, value): d[key] = value
if name == 'main': mgr = multiprocessing.Manager() d = mgr.dict() jobs = [multiprocessing.Process(target=worker, args=(d, i, i * 2)) for i in range(10) ] for j in jobs: j.start() for j in jobs: j.join() print('Results:') for key, value in enumerate(dict(d)): print("%s=%s" % (key, value))
multiprocessing.Manager()
mgr.dict() 这个对象可以在进程间共享数据。 进程间共享数据的方式:队列,管道,Manager()