Skip to content

Instantly share code, notes, and snippets.

@Antrikshy
Created February 21, 2022 00:49
Show Gist options
  • Save Antrikshy/18b458fef8ad37d437c6a0b45b89d78c to your computer and use it in GitHub Desktop.
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
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