[python] How do I capture SIGINT in Python?

I adapted the code from @udi to support multiple signals (nothing fancy) :

class GracefulInterruptHandler(object):
    def __init__(self, signals=(signal.SIGINT, signal.SIGTERM)):
        self.signals = signals
        self.original_handlers = {}

    def __enter__(self):
        self.interrupted = False
        self.released = False

        for sig in self.signals:
            self.original_handlers[sig] = signal.getsignal(sig)
            signal.signal(sig, self.handler)

        return self

    def handler(self, signum, frame):
        self.release()
        self.interrupted = True

    def __exit__(self, type, value, tb):
        self.release()

    def release(self):
        if self.released:
            return False

        for sig in self.signals:
            signal.signal(sig, self.original_handlers[sig])

        self.released = True
        return True

This code support the keyboard interrupt call (SIGINT) and the SIGTERM (kill <process>)

Examples related to python

programming a servo thru a barometer Is there a way to view two blocks of code from the same file simultaneously in Sublime Text? python variable NameError Why my regexp for hyphenated words doesn't work? Comparing a variable with a string python not working when redirecting from bash script is it possible to add colors to python output? Get Public URL for File - Google Cloud Storage - App Engine (Python) Real time face detection OpenCV, Python xlrd.biffh.XLRDError: Excel xlsx file; not supported Could not load dynamic library 'cudart64_101.dll' on tensorflow CPU-only installation

Examples related to controls

What is Dependency Injection and Inversion of Control in Spring Framework? Change the location of an object programmatically How to access Winform textbox control from another class? How to make overlay control above all other controls? How to get ALL child controls of a Windows Forms form of a specific type (Button/Textbox)? How can I change the Java Runtime Version on Windows (7)? How can I make visible an invisible control with jquery? (hide and show not work) DataGridView - how to set column width? Get a Windows Forms control by name in C# C# Get a control's position on a form

Examples related to signals

Is it possible to capture a Ctrl+C signal and run a cleanup function, in a "defer" fashion? How to suspend/resume a process in Windows? How to trigger SIGUSR1 and SIGUSR2? Catch Ctrl-C in C How can I catch a ctrl-c event? How do I capture SIGINT in Python? Can I send a ctrl-C (SIGINT) to an application on Windows? What killed my process and why? What's the best way to send a signal to all members of a process group? How to prevent SIGPIPEs (or handle them properly)