我无法收集所有池进程的输出。结果字典可能包含 3 个并行进程中的 1 个的输出(411 个元素中的 288 个)。 这是我的代码。
from itertools import repeat
from multiprocessing import Pool, Manager
def reader(entry, outcomes): #other args are constant
.............................................
#Some condition:
prediction = min(distances) + (mlc_data[min(distances)[1]],)
outcomes[entry_pairs[' '.join(entry)]] = prediction
return outcomes
manager = Manager()
outcomes = manager.dict()
with Pool(3) as p:
output = p.starmap(reader, zip(input[0], repeat(outcomes)))
p.close()
p.join()
最后,结果字典中的 411 个元素中只有 288 个。 从另一边来看,我的输出中有 411**2 个元素,这可能意味着我的代码没有得到很好的优化。
使用共享字典
reader
更新outcomes
函数并使用Pool
from itertools import repeat
from multiprocessing import Pool, Manager
def reader(entry, outcomes): # other args are constant
# YOUR CODE HERE
prediction = min(distances) + (mlc_data[min(distances)[1]],)
outcomes[entry_pairs[' '.join(entry)]] = prediction
manager = Manager()
outcomes = manager.dict()
with Pool(3) as p:
p.starmap(reader, zip(input[0], repeat(outcomes)))
print(len(outcomes))