我有一份我希望分发给池中工作人员的任务列表。我想实现两件事:
使用fapply_async,我可以轻松实现第一个目标。每当工作者完成时,都会调用回调。但是,要实现第二个目标,我能提出的唯一解决方案基本上只是轮询AsyncResults,直到它们都准备就绪()。
使用map_async,我可以轻松实现第二个目标。但是,当所有工作程序完成时,回调仅被调用一次。我相信我理解这个的原因(结果的顺序是相关的)。
我是否缺少一些可以实现目标1和目标2的解决方案?
这是我的测试代码:
#!/usr/bin/python3
import multiprocessing
import time
import random
def worker(src):
time.sleep(0.2)
# src is apply_async or map_async
return (src, random.randint(1, 100))
def map_async_example():
tasks = ['map_async'] * 20
with multiprocessing.Pool(processes=4) as pool:
r = pool.map_async(worker, tasks, callback=print)
r.wait()
def fapply_async_example():
tasks = [('fapply_async',)] * 20
with multiprocessing.Pool(processes=4) as pool:
ars = []
for t in tasks:
ar = pool.apply_async(worker, t, callback=print)
ars.append(ar)
# Wait for all AsyncResults to become ready()
while len(ars) > 0:
time.sleep(0.5)
# Keep only the not-ready results
ars = [ar for ar in ars if not ar.ready()]
def main():
# One list of 20 results
print('===============')
print('Using map_async')
print('===============')
map_async_example()
# 20 results
print('==================')
print('Using fapply_async')
print('==================')
fapply_async_example()
if __name__ == '__main__':
main()