Let's say I have this script with a function to handle my interrupt signal like so:
import signal
import time
import sys
def handle_interrupt(signum, frame):
print('Gracefully exiting in 5 seconds...')
time.sleep(5)
sys.exit()
signal.signal(signal.SIGINT, handle_interrupt)
print('Sleeping for 5 minutes...')
time.sleep(300)
How can I handle a second interrupt signal so that it quits instantly without waiting 5 additional seconds?
The best way to do it would be to define an interrupt handler object to manage the state of whether the interrupt signal has been received or not already.
Here's another solution using a global variable to store the "interrupted" state. Using global variables is generally considered bad practice, however.