gpt4 book ai didi

python - `with` 中 `concurrent.futures` 的功能

转载 作者:行者123 更新时间:2023-12-03 12:43:51 25 4
gpt4 key购买 nike

下面的代码通过返回几乎为零的总完成时间按预期执行,因为它不等待线程完成每个作业。

import concurrent.futures
import time

start = time.perf_counter()


def do_something(seconds):
print(f'Sleeping {seconds} second(s)...')
time.sleep(seconds)
return f'Done Sleeping...{seconds}'



executor= concurrent.futures.ThreadPoolExecutor()
secs = [10, 4, 3, 2, 1]

fs = [executor.submit(do_something, sec) for sec in secs]


finish = time.perf_counter()

print(f'Finished in {round(finish-start, 2)} second(s)')

但是使用 with命令它确实等待:
with concurrent.futures.ThreadPoolExecutor() as executor:
secs = [10, 4, 3, 2, 1]

fs = [executor.submit(do_something, sec) for sec in secs]

为什么?是什么原因 with多线程有这种行为吗?

最佳答案

使用 concurrent.futures.Executor with statement相当于调用 Executor.shutdown使用后——导致执行者等待所有任务完成。一个 Executor用于 with即使在 with 内部发生错误,也保证正确关闭并发任务堵塞。

Executor.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.

[...]

You can avoid having to call this method explicitly if you use the with statement, which will shutdown the Executor (waiting as if Executor.shutdown() were called with wait set to True): [...]

关于python - `with` 中 `concurrent.futures` 的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62105222/

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