我该怎么等线程池执行器.map完成

2024-10-01 17:39:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下经过简化的代码:

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

关于池映射执行器. 帮个忙就好了。在

谢谢!在


Tags: 代码inimportmapforreturnisdef
2条回答

执行器.map将并行运行作业并等待未来完成,收集结果并返回生成器。它已经等你了。如果您设置了超时,它将一直等到超时并在生成器中抛出异常。在

map(func, *iterables, timeout=None, chunksize=1)

  • the iterables are collected immediately rather than lazily;
  • func is executed asynchronously and several calls to func may be made concurrently.

要获取期货列表并手动等待,可以使用:

myfuturelist = [pool.submit(_exec, x) for x in range(5)]

执行人.提交将返回一个future对象,在将来调用result将显式等待它完成:

^{pr2}$

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是正确的方法。在

相关问题 更多 >

    热门问题