使用方法ThreadPoolExecutor.shutdown(wait = True),shutdown(wait = False)和不使用这个方法有什么区别?

时间:2015-02-09 19:16:41

标签: 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 ?

1 个答案:

答案 0 :(得分:4)

来自文档:

  

如果wait为True,则此方法将不会返回,直到所有待处理的期货完成执行并且已释放与执行程序关联的资源。如果wait为False,则此方法将立即返回,并且当执行所有待处理的期货时,将释放与执行程序关联的资源。无论wait的值如何,整个Python程序都不会退出,直到所有待处理的期货都执行完毕。

这包括前两个例子。

对于第三个,因为ThreadPoolExecutor遵守"上下文管理器"您可以在with语句中使用它,以便在执行退出shutdown块时自动调用with方法。

如果省略参数,默认值为True - 或者如果您将其用作上下文管理器,那么无论with的值如何,在wait块中使用它都是无用的

[编辑]

  

我编辑了代码。请参阅,有我最后的问题

如果您要明确释放所有资源并确保没有对shutdownsubmit的新来电成功,则只需调用map方法。如果您不调用shutdown(或使用ThreadPoolExecutor作为上下文管理器),则只有在整个Python程序退出时才会释放资源(并且在完成所有挂起的未来之前它不会退出)。

使用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))