我应该如何立即终止所有任务

时间:2019-06-12 12:23:58

标签: python threadpoolexecutor

我现在使用ThreadPoolExecutor执行一些任务:

executor = ThreadPoolExecutor(max_workers=len(xxx))
tasks = [executor.submit(xxx) for x in xxx]

然后,如果超时,我想终止所有任务:

while not all_tasks_finished(tasks):
    if datetime.now() - start_time > timedelta(seconds=60):
        # executor.shutdown(False)??

似乎executor.shutdown()直到所有任务完成才执行。我想知道是否有任何功能可以终止未完成的任务

1 个答案:

答案 0 :(得分:0)

首先尝试提交提交的canceling the Futures

while not all_tasks_finished(tasks):
    if datetime.now() - start_time > timedelta(seconds=60):
        for t in tasks:
            t.cancel()
        executor.shutdown()