首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用ThreadPoolExecutor优雅退出

使用ThreadPoolExecutor优雅退出
EN

Stack Overflow用户
提问于 2020-12-25 06:09:20
回答 1查看 484关注 0票数 1

在使用ThreadPoolExecutor时,如何在信号中断时正常退出?我想截取一个SIGINT并优雅地退出进程。我希望当前运行的线程完成,但不再启动,并取消所有挂起的任务。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-25 06:09:20

在Python3.9中,ThreadPoolExecutor#shutdown接受一个cancel_futures参数来处理取消等待任务。然而,在Python3.8及更低版本中,这必须手动处理。幸运的是,我们可以自己使用the code to do this in python 3.9

代码语言:javascript
复制
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...
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65443612

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档