在使用ThreadPoolExecutor时,如何在信号中断时正常退出?我想截取一个SIGINT并优雅地退出进程。我希望当前运行的线程完成,但不再启动,并取消所有挂起的任务。
发布于 2020-12-25 06:09:20
在Python3.9中,ThreadPoolExecutor#shutdown接受一个cancel_futures参数来处理取消等待任务。然而,在Python3.8及更低版本中,这必须手动处理。幸运的是,我们可以自己使用the code to do this in python 3.9。
import sys
import queue
from concurrent.futures import ThreadPoolExecutor
def exit_threads( executor ):
print( '\nWrapping up, please wait...' )
py_version = sys.version_info
if ( py_version.major == 3 ) and ( py_version.minor < 9 ):
# py versions less than 3.9
# Executor#shutdown does not accept
# cancel_futures keyword
# manually shutdown
# code taken from https://github.com/python/cpython/blob/3.9/Lib/concurrent/futures/thread.py#L210
# prevent new tasks from being submitted
executor.shutdown( wait = False )
while True:
# cancel all waiting tasks
try:
work_item = executor._work_queue.get_nowait()
except queue.Empty:
break
if work_item is not None:
work_item.future.cancel()
else:
executor.shutdown( cancel_futures = True )
sys.exit( 0 )
executor = ThreadPoolExecutor( max_workers = 5 )
signal.signal(
signal.SIGINT,
lambda sig, frame: exit_threads( executor )
)
# run desired code here...https://stackoverflow.com/questions/65443612
复制相似问题