ctrlc.py 文件源码

python
阅读 28 收藏 0 点赞 0 评论 0

项目:phredutils 作者: doctaphred 项目源码 文件源码
def prompt(cleanup_function, signalnum=signal.SIGINT):
    """Give the user a decision prompt when ctrl+C is pressed."""
    def cleanup_prompt(signum, frame):
        # This handler is not re-entrant.
        # Allow the user to exit immediately by sending the same signal
        # a second time while in this block.
        with handle(signalnum, sysexit):
            choice = input(dedent("""
                You pressed ctrl+C. What would you like to do?
                    - to exit immediately, press ctrl+C again
                    - to clean up and exit gracefully, enter 'c' or 'cleanup'
                    - to resume operation, press enter
                (cleanup?): """))
            if choice and 'cleanup'.casefold().startswith(choice.casefold()):
                print('Cleaning up...')
                cleanup_function()
                print('Done cleaning up; exiting.')
                sys.exit(1)
        print('Continuing...')

    return cleanup_prompt
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号