我想使用pool.apply_async进行异步处理,但它会继续运行而不返回结果。
def myfun(x, y, i ,j):
z = process data(x, y)
return i, j, z
from multiprocessing import Pool
pool = Pool(processes=4)
res = []
for i in range(10):
x = df.loc[i, 'data']
for j in range(i+1, 10):
y = df.loc[j, 'data']
res.append(pool.apply_async(myfun, (x, y, i, j)))
for i in res:
print(i.get())
如何获得正确的解决方案?
答案 0 :(得分:1)
您需要close
池并等待进程使用join
完成:
def myfun(x, y, i ,j):
z = process data(x, y)
return i, j, z
from multiprocessing import Pool
pool = Pool(processes=4)
res = []
for i in range(10):
x = df.loc[i, 'data']
for j in range(i+1, 10):
y = df.loc[j, 'data']
res.append(pool.apply_async(myfun, (x, y, i, j)))
pool.close()
pool.join()
for i in res:
print(i.get())