我有以下经过简化的代码:
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 的文档薄弱。帮助会很棒。
谢谢!
最佳答案
对 ThreadPoolExecutor.map
的调用不会阻塞,直到它的所有任务都完成。使用 wait做这个。
from concurrent.futures import wait, ALL_COMPLETED
...
futures = [pool.submit(fn, args) for args in arg_list]
wait(futures, timeout=whatever, return_when=ALL_COMPLETED) # ALL_COMPLETED is actually the default
do_other_stuff()
您还可以在 pool.map
返回的生成器上调用 list(results)
以强制执行评估(这是您在原始示例中所做的) .但是,如果您实际上没有使用任务返回的值,wait
是可行的方法。
关于python - 我如何等待 ThreadPoolExecutor.map 完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48532411/