How do I capture SIGINT in Python?

前端 未结 12 1764
长发绾君心
长发绾君心 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:10

    From Python's documentation:

    import signal
    import time
    
    def handler(signum, frame):
        print 'Here you go'
    
    signal.signal(signal.SIGINT, handler)
    
    time.sleep(10) # Press Ctrl+c here
    

提交回复
热议问题