如何等待ThreadPoolExecutor.map完成
问题描述
我有以下经过简化的代码:
import concurrent.futures
pool = concurrent.futures.ThreadPoolExecutor(8)
def _exec(x):
return x + x
myfuturelist = pool.map(_exec,[x for x in range(5)])
# How do I wait for my futures to finish?
for result in myfuturelist:
# Is this how it's done?
print(result)
#... stuff that should happen only after myfuturelist is
#completely resolved.
# Documentation says pool.map is asynchronous
关于ThreadPoolExecutor.map的文档很弱。如果能帮上忙就太好了。
谢谢!
解决方案
Executor.map将并行运行作业,并等待Futures完成、收集结果并返回生成器。它已经为你做了等待。如果设置超时,它将一直等到超时,并在生成器中引发异常。
map(函数,*可迭代数,超时=无,块大小=1)
- 立即收集可迭代对象,而不是延迟收集;
- Func是异步执行的,可以同时调用Func。
要获取期货列表并手动执行等待,您可以使用:
myfuturelist = [pool.submit(_exec, x) for x in range(5)]
Executor.Submit将返回future对象,将来调用result
将显式等待其完成:
myfutrelist[0].result() # wait the 1st future to finish and return the result
相关文章