Skip to content

Instantly share code, notes, and snippets.

@TuranTimur
Last active January 16, 2016 07:29
Show Gist options
  • Save TuranTimur/371e4fbcd5bc5fd4e93d to your computer and use it in GitHub Desktop.
Save TuranTimur/371e4fbcd5bc5fd4e93d to your computer and use it in GitHub Desktop.
import signal, os, time
def handler(signum, frame):
print 'Signal handler called with signal', signum
raise IOError("Couldn't open device!")
# Set the signal handler and a 5-second alarm
signal.signal(signal.SIGALRM, handler)
signal.alarm(5)
time.sleep(6) # long task like handling file system or device
signal.alarm(0) # Disable the alarm
########################################################################
# run result of python signal_test.py
# Signal handler called with signal 14
# Traceback (most recent call last):
# File "signal_test.py", line 11, in <module>
# time.sleep(6)
# File "signal_test.py", line 5, in handler
# raise IOError("Couldn't open device!")
#IOError: Couldn't open device!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment