This code will set an "alarm" that must be set every 30 seconds, or else a signal will be called to shut down the program (sort of a "watchdog" program).
def alarm_handler(signum, frame):
print "30 seconds have passed!"
signal.signal(signal.SIGALRM, alarm_handler)
signal.alarm(30)
while True:
data = raw_input("Write something in the next 30 seconds please: ")
signal.alarm(30)
Another use of signals would be to brutally terminate your program, since sometimes Python can hang on I/O, or a Thread object that is running (for some reason a Ctrl+C doesn't kill a program running a python thread):
import os, signal
# Brutally terminate this process
os.kill(0, signal.SIGTERM)
No comments:
Post a Comment