【发布时间】:2015-02-03 08:42:26
【问题描述】:
我想了解多处理和管理器如何工作以共享内存
我有一个类,其中包含在类上创建的字典。init 我想使用多处理来调用填充字典的类函数(每个进程添加一个不同的键)。
import multiprocessing as mp
from multiprocessing import Process, Manager
class num:
def __init__(self):
manager = Manager()
d = manager.dict()
# Setup list of processes
processes = [mp.Process(target=self.f, args=(d,i)) for i in range(5)]
#Run processes
for p in processes:
p.start()
#Exit the completed processes
for p in processes:
p.join()
print d
def f(self,d,i):
d[str(i)] = []
d[str(i)].append(i)
if __name__ == '__main__':
test = num()
结果:
{'1': [], '0': [], '3': [], '2': [], '4': []}
f() 中的列表也需要共享?如何以及为什么?
【问题讨论】:
-
DNA 回答后的问题更新,以便更具体
标签: python dictionary shared-memory