我正在做一个CPU昂贵的计算,可以很容易地分成许多工人。但是,我必须在最后加入结果。的问题是,不能使用使用多个分离的过程时发送从子到父计算机输出的唯一方法是使用multiprocessing.Queue
,multiprocessing.Manager
或multiprocessing.Pool
共享内存和。所有这些方法腌制物体并使用某种IPC发送它。将多个字典返回给父处理多处理
我计时操作的所有不同部分,和处理数据与处理方式更快,但是得到的对象是太慢了,它总是更快,如果我不使用多。
有没有办法实现共享内存使用我们当multithreading library
得到相同级别的?我希望能够做一些事情,如:,我建议使用该萝模块
process = [None]*numProcess
#List where the processes should write in memory the output.
results = [None]*numProcess
m = float(len(nflow))/numProcess
nflow_for_process = [nflow[int(m*i):int(m*(i+1))] for i in range(numProcess)]
for i in xrange(numProcess):
p = Process(target=self.gatherFlowsProcess, args=(nflow_for_process[i]))
p.daemon = True
processes.append(p)
p.start()
#here I join all the results again.
results_tmp = results[0]
for d in results[1:]:
for tuple in d:
if results_tmp.has_key(tuple):
results_tmp[tuple].update(d[tuple])
else:
results_tmp[tuple] = d[tuple]
return results_tmp
我觉得词典解决您的问题。 –
字典在哪里?一本字典,它的孩子必须返回,或将其追加到列表中。然而,由于他们没有共享内存,我找不到一个有效的方法来做到这一点。 Pickle需要大量的时间来腌制返回的字典。 – edgarstack