从文档:
如果 wait 为 True,则此方法将不会返回,直到所有未决的期货都执行完毕并且与执行程序关联的资源已被释放。如果 wait 为 False,则此方法将立即返回,并且当所有挂起的期货执行完毕后,与执行程序关联的资源将被释放。无论 wait 的值如何,整个 Python 程序都不会退出,直到所有未决的期货都执行完毕。
这涵盖了前两个示例。
对于第三个,由于ThreadPoolExecutor
遵循“上下文管理器”协议,您可以在with
语句中使用它,以便在shutdown
执行退出with
块时自动调用该方法。
默认情况下,True
如果您省略参数 - 或者如果您将其用作上下文管理器,因此在with
块内使用它是无用的,无论wait
.
[编辑]
我编辑了代码。请看,这是我最后的问题
shutdown
如果您想显式释放所有资源并确保没有新的调用submit
或map
将成功,则仅调用该方法。如果不调用shutdown(或ThreadPoolExecutor
用作上下文管理器),则可能仅在整个Python程序退出时才释放资源(并且直到所有未决的future完成后才会退出)。
shutdown
使用上下文管理器调用wait==True
或ThreadPoolExecutor
用作上下文管理器将阻塞,直到所有未决的期货都执行完毕。
我能想到的shutdown
显式调用的唯一用例是:
executor = ThreadPoolExecutor(4)
try:
executor.map(func, tasks)
finally:
executor.shutdown(wait=False)
为了给出一些上下文,这是这个问题的第一个版本的代码片段:
def func(task):
do_something(task)
tasks = [task for i in range(12)]
with ThreadPoolExecutor(4) as executor:
executor.map(func, tasks)
executor.shutdown(wait=True) # what is happening here?
tasks = [task for i in range(12)]
with ThreadPoolExecutor(4) as executor:
executor.map(func, tasks)
executor.shutdown(wait=False) # what is happening here?
tasks = [task for i in range(12)]
with ThreadPoolExecutor(4) as executor:
executor.map(func, tasks) # and without shutdown()?
请注意,这tasks = [task for i in range(12)]
是多余的 - 您也可以使用executor.map(func, range(12))
。