How do I capture SIGINT in Python?

前端 未结 12 1511
长发绾君心
长发绾君心 2020-11-21 22:54

I\'m working on a python script that starts several processes and database connections. Every now and then I want to kill the script with a Ctrl+C sign

12条回答
  •  情话喂你
    2020-11-21 23:12

    Yet Another Snippet

    Referred main as the main function and exit_gracefully as the CTRL + c handler

    if __name__ == '__main__':
        try:
            main()
        except KeyboardInterrupt:
            pass
        finally:
            exit_gracefully()
    

提交回复
热议问题