Created
June 14, 2011 22:15
-
-
Save FurryHead/1026067 to your computer and use it in GitHub Desktop.
Timeout thread.
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 threading | |
class TimeoutException(Exception): | |
pass | |
class TimeoutThread(threading.Thread): | |
def run(self): | |
def timeout_handler(signum, frame): | |
raise TimeoutException() | |
old_handler = signal.signal(signal.SIGALRM, timeout_handler) | |
signal.alarm(10) | |
try: | |
pass #Code here! | |
except TimeoutException: | |
print "Timed out" | |
finally: | |
signal.signal(signal.SIGALRM, old_handler) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment