Add information about signal handler

This commit is contained in:
exu 2024-06-05 20:13:06 +02:00
parent 8fc06a1453
commit 7c3129a1b2

View File

@ -45,3 +45,22 @@ except KeyboardInterrupt:
# return SIGINT code # return SIGINT code
exit(130) exit(130)
``` ```
A potentially better way of handling keyboard interrupts is using a signal handler.
```python
import time
import signal
import sys
def signal_handler(signal, frame):
print("\nKeyboard Interrupt detected.")
# add your cleanup code here
sys.exit(0)
# register signal handler
signal.signal(signal.SIGINT, signal_handler)
while True:
time.sleep(1)
```