Skip to content

Instantly share code, notes, and snippets.

@dbr
Created December 5, 2008 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbr/32398 to your computer and use it in GitHub Desktop.
Save dbr/32398 to your computer and use it in GitHub Desktop.
signal based timeout for function call
#!/usr/bin/env python
class TimeExceededError(Exception):pass
def run_cmd(cmd, timeout = 0):
import signal
def alarmHandler(signum, frame):
raise TimeExceededError, "Took too long to execute"
signal.signal(signal.SIGALRM, alarmHandler)
signal.alarm(1)
cmd()
def myfunc():
import time
time.sleep(2)
return
try:
run_cmd(myfunc)
except TimeExceededError:
print "Function timed out!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment