CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
timeout.py
Go to the documentation of this file.
1 import signal, time
2 
4  def __init__(self, value = "Timed Out"):
5  self.value = value
6  def __str__(self):
7  return repr(self.value)
8 
9 def TimedOutFn(f, timeout, *args, **kwargs):
10  def handler(signum, frame):
11  raise TimedOutExc()
12 
13  old = signal.signal(signal.SIGALRM, handler)
14  signal.alarm(timeout)
15  try:
16  result = f(*args, **kwargs)
17  finally:
18  signal.signal(signal.SIGALRM, old)
19  signal.alarm(0)
20  return result
21 
22 
23 def timed_out(timeout):
24  def decorate(f):
25  def handler(signum, frame):
26  raise TimedOutExc()
27 
28  def new_f(*args, **kwargs):
29  old = signal.signal(signal.SIGALRM, handler)
30  signal.alarm(timeout)
31 
32  time_up = True
33  try:
34  result = f(*args, **kwargs)
35  time_up = False
36  finally:
37  signal.signal(signal.SIGALRM, old)
38  signal.alarm(0)
39  if time_up:
40  raise TimedOutExc()
41  return result
42 
43  new_f.__name__ = f.__name__
44  return new_f
45 
46  return decorate
def timed_out
Definition: timeout.py:23
def TimedOutFn
Definition: timeout.py:9