How do I capture SIGINT in Python?

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

    In contrast to Matt J his answer, I use a simple object. This gives me the possibily to parse this handler to all the threads that needs to be stopped securlery.

    class SIGINT_handler():
        def __init__(self):
            self.SIGINT = False
    
        def signal_handler(self, signal, frame):
            print('You pressed Ctrl+C!')
            self.SIGINT = True
    
    
    handler = SIGINT_handler()
    signal.signal(signal.SIGINT, handler.signal_handler)
    

    Elsewhere

    while True:
        # task
        if handler.SIGINT:
            break
    
    0 讨论(0)
  • thanks for existing answers, but added signal.getsignal()

    import signal
    
    # store default handler of signal.SIGINT
    default_handler = signal.getsignal(signal.SIGINT)
    catch_count = 0
    
    def handler(signum, frame):
        global default_handler, catch_count
        catch_count += 1
        print ('wait:', catch_count)
        if catch_count > 3:
            # recover handler for signal.SIGINT
            signal.signal(signal.SIGINT, default_handler)
            print('expecting KeyboardInterrupt')
    
    signal.signal(signal.SIGINT, handler)
    print('Press Ctrl+c here')
    
    while True:
        pass
    
    0 讨论(0)
  • 2020-11-21 23:30

    You can use the functions in Python's built-in signal module to set up signal handlers in python. Specifically the signal.signal(signalnum, handler) function is used to register the handler function for signal signalnum.

    0 讨论(0)
  • 2020-11-21 23:32

    You can treat it like an exception (KeyboardInterrupt), like any other. Make a new file and run it from your shell with the following contents to see what I mean:

    import time, sys
    
    x = 1
    while True:
        try:
            print x
            time.sleep(.3)
            x += 1
        except KeyboardInterrupt:
            print "Bye"
            sys.exit()
    
    0 讨论(0)
  • 2020-11-21 23:32

    If you want to ensure that your cleanup process finishes I would add on to Matt J's answer by using a SIG_IGN so that further SIGINT are ignored which will prevent your cleanup from being interrupted.

    import signal
    import sys
    
    def signal_handler(signum, frame):
        signal.signal(signum, signal.SIG_IGN) # ignore additional signals
        cleanup() # give your process a chance to clean up
        sys.exit(0)
    
    signal.signal(signal.SIGINT, signal_handler) # register the signal with the signal handler first
    do_stuff()
    
    0 讨论(0)
  • 2020-11-21 23:36

    Register your handler with signal.signal like this:

    #!/usr/bin/env python
    import signal
    import sys
    
    def signal_handler(sig, frame):
        print('You pressed Ctrl+C!')
        sys.exit(0)
    signal.signal(signal.SIGINT, signal_handler)
    print('Press Ctrl+C')
    signal.pause()
    

    Code adapted from here.

    More documentation on signal can be found here.  

    0 讨论(0)
提交回复
热议问题