无法使用pool.apply_async获取结果

时间:2018-01-15 09:32:22

标签: python multithreading

我想使用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())

如何获得正确的解决方案?

1 个答案:

答案 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())