Created
February 21, 2022 00:49
-
-
Save Antrikshy/18b458fef8ad37d437c6a0b45b89d78c to your computer and use it in GitHub Desktop.
Python snippet for elegantly handling interruptions, like keyboard interrupts, in simple scripts that can be structured around this
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import signal | |
import sys | |
def _handle_interrupt(signum, _): | |
signal.signal(signum, signal.SIG_IGN) | |
# Any cleanup steps go here | |
sys.exit(signal.SIGINT) | |
signal.signal(signal.SIGINT, _handle_interrupt) | |
# Rest of script... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment