Last active
January 16, 2016 07:29
-
-
Save TuranTimur/371e4fbcd5bc5fd4e93d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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