In Python you can handle Ctrl+C with KeyboardInterrupt, but how do you handle Ctrl+Break?
Asked
Active
Viewed 1,784 times
1 Answers
0
If you want to ignore it then you can add a signal handler like so:
signal.signal(signal.SIGBREAK, signal.SIG_IGN)
If you want to gracefully shut down your process then you can create a separate signal handler function that will be called whenever the signal is received like so:
import signal
def signal_handler(self, signum, frame):
print(f"Received {signum}! Shutting down...")
self.cleanup() # create your own clean up function
sys.exit()
if __name__ == "__main__":
signal.signal(signal.SIGBREAK, signal_handler)
# do stuff here in main

Josh Correia
- 3,807
- 3
- 33
- 50