I have a script that I was trying to add a signal handler for ctr-c, to do some final handling before exiting.
On it's own, it works fine but as soon as I try it in my program, it doesn't work. Adding or removing an import seems to change the behavior.
Without the P4API import, it works as I expect. If I do import P4API, ctr-c seems to call exit or bypass my handler, and I'm not sure why, or how to track what is changing.
import signal
import time
import sys
# -- with this commented out, things work
#import P4API
def run_program():
while True:
time.sleep(1)
print("a")
def exit_gracefully(signum, frame):
# restore the original signal handler as otherwise evil things will happen
# in raw_input when CTRL+C is pressed, and our signal handler is not re-entrant
signal.signal(signal.SIGINT, original_sigint)
try:
if raw_input("\nReally quit? (y/n)> ").lower().startswith('y'):
sys.exit(1)
except KeyboardInterrupt:
print("Ok ok, quitting")
sys.exit(1)
# restore the exit gracefully handler here
signal.signal(signal.SIGINT, exit_gracefully)
if __name__ == '__main__':
# store the original SIGINT handler
original_sigint = signal.getsignal(signal.SIGINT)
signal.signal(signal.SIGINT, exit_gracefully)
run_program()