我正在做一個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