Skip to content

Instantly share code, notes, and snippets.

@FurryHead
Created June 14, 2011 22:15
Show Gist options
  • Save FurryHead/1026067 to your computer and use it in GitHub Desktop.
Save FurryHead/1026067 to your computer and use it in GitHub Desktop.
Timeout thread.
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