r45*_*45i 5 python concurrent.futures
我无法理解差异。帮助我观察这种差异。而ProcessPoolExecutor呢,他的行为是不是一样?
def func(task):
do_something(task)
tasks = [task for i in range(12)]
executor = ThreadPoolExecutor(4)
executor.map(func, tasks)
executor.shutdown(wait=True) # ok, here the main thread waits for others
tasks = [task for i in range(12)]
executor = ThreadPoolExecutor(4)
executor.map(func, tasks)
executor.shutdown(wait=False) # here it doesn't wait and what can happens bad?
tasks = [task for i in range(12)]
executor = ThreadPoolExecutor(4)
executor.map(func, tasks) # if i don't call shutdown ?
Run Code Online (Sandbox Code Playgroud)
从文档:
如果 wait 为 True 则此方法将不会返回,直到所有挂起的期货都执行完毕并且与执行程序关联的资源已被释放。如果 wait 为 False,则此方法将立即返回,并且当所有挂起的期货执行完毕后,将释放与执行程序关联的资源。不管wait的值是多少,在所有挂起的future执行完成之前,整个Python程序都不会退出。
这涵盖了前两个示例。
对于第三个,由于ThreadPoolExecutor遵守“上下文管理器”协议,您可以在with语句中使用它,以便在shutdown执行退出with块时自动调用该方法。
默认情况下,True如果您省略参数 - 或者如果您将其用作上下文管理器,因此with无论wait.
[编辑]
我编辑了代码。请参阅,这是我最后的问题
shutdown如果您想显式释放所有资源并确保没有新的调用submit或map将成功,则仅调用该方法。如果不调用shutdown(或ThreadPoolExecutor用作上下文管理器),则可能只有在整个Python程序退出时才会释放资源(并且在所有挂起的期货完成后才会退出)。
调用shutdown与wait==True或使用ThreadPoolExecutor的上下文管理器将阻塞,直到所有未决期货执行完毕。
我能想到的shutdown显式调用的唯一用例是:
executor = ThreadPoolExecutor(4)
try:
executor.map(func, tasks)
finally:
executor.shutdown(wait=False)
Run Code Online (Sandbox Code Playgroud)
为了给出一些上下文,这是这个问题的第一个版本的代码片段:
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()?
Run Code Online (Sandbox Code Playgroud)
请注意,这tasks = [task for i in range(12)]是多余的 - 您也可以使用executor.map(func, range(12))。