我在以下代码中做错了什么?
import multiprocessing as mp
from multiprocessing import Process
import numpy as np
def fun(X):
return X;
def funct(p,i):
print 'Hey'
res = [p.map(fun,range(2))];
return res;
if __name__ == '__main__':
pool = mp.Pool(2);
output = mp.Queue();
proc = [mp.Process(target = funct,args=(pool,i)) for i in range(2)]
for p in proc:
p.start()
for p in proc:
p.join()
results = [output.get() for p in proc]
print results
启动该过程后,该程序未运行.
解决方法:
multiprocessing.Pool不支持将其传递给子进程,如multiprocessing
documentation中所述:
Note that the methods of a pool should only ever be used by the process which created it.
因此,实际上没有办法做您要尝试的事情.我假设上面的代码只是一个示例来说明问题,所以我不会直接对此进行评论,但是您需要重构实际代码以仅从父进程中调用池方法(或可能创建在孩子本身中集中,但这可能不是一个好的解决方案).