3

In Python you can handle Ctrl+C with KeyboardInterrupt, but how do you handle Ctrl+Break?

Josh Correia
  • 3,807
  • 3
  • 33
  • 50
Junior
  • 165
  • 1
  • 1
  • 10

1 Answers1

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