gpt4 book ai didi

Python:WAITING所有 `concurrent.futures.ThreadPoolExecutor` 的 future

转载 作者:IT老高 更新时间:2023-10-28 21:58:30 25 4
gpt4 key购买 nike

我已经给 concurrent.futures.ThreadPoolExecutor 一堆任务,我想等到它们都完成后再继续流程。我怎样才能做到这一点,而不必保存所有 future 并对其调用 wait ? (我想对执行者采取行动。)

最佳答案

只需调用 Executor.shutdown :

shutdown(wait=True)

Signal the executor that it should free any resources that it is using when the currently pending futures are done executing. Calls to Executor.submit() and Executor.map() made after shutdown will raise RuntimeError.

If wait is True then this method will not return until all the pending futures are done executing and the resources associated with the executor have been freed.

但是,如果您在列表中跟踪您的 future ,则可以使用 futures.wait() 避免关闭执行程序以备将来使用。功能:

concurrent.futures.wait(fs, timeout=None, return_when=ALL_COMPLETED)

Wait for the Future instances (possibly created by different Executor instances) given by fs to complete. Returns a named 2-tuple of sets. The first set, named done, contains the futures that completed (finished or were cancelled) before the wait completed. The second set, named not_done, contains uncompleted futures.

请注意,如果您不提供 timeout,它会等到所有 future 都完成。

您也可以使用futures.as_completed()相反,但是您必须对其进行迭代。

关于Python:WAITING所有 `concurrent.futures.ThreadPoolExecutor` 的 future ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21143162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com