I just mentioned signal , which is also built-in, which can handle Ctrl+ Cand many more signals, for example SIGHUP, etc.
import signal
def signal_handler(signal, frame):
exit(0)
signal.signal(signal.SIGINT, signal_handler)
This is just an example of a wide structure that can handle multiple signals.
Here is a list of some signals you could catch.
source
share