timeout.py
Go to the documentation of this file.
1 from functools import wraps
2 import errno
3 import os
4 import signal
5 
6 class TimeoutError(Exception):
7  pass
8 
9 def timeout(seconds=10, error_message=os.strerror(errno.ETIME)):
10  def decorator(func):
11  def _handle_timeout(signum, frame):
12  raise TimeoutError(error_message)
13 
14  def wrapper(*args, **kwargs):
15  signal.signal(signal.SIGALRM, _handle_timeout)
16  signal.alarm(seconds)
17  try:
18  result = func(*args, **kwargs)
19  finally:
20  signal.alarm(0)
21  return result
22 
23  return wraps(func)(wrapper)
24 
25  return decorator
def timeout(seconds=10, error_message=os.strerror(errno.ETIME))
Definition: timeout.py:9


dyn_tune
Author(s):
autogenerated on Mon Jun 10 2019 13:03:17