This repository has been archived by the owner on Apr 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
timeout.py
89 lines (72 loc) · 2.33 KB
/
timeout.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
## {{{ http://code.activestate.com/recipes/577853/ (r1)
"""
Code to timeout with processes.
>>> @timeout(.5)
... def sleep(x):
... print "ABOUT TO SLEEP {0} SECONDS".format(x)
... time.sleep(x)
... return x
>>> sleep(1)
Traceback (most recent call last):
...
TimeoutException: timed out after 0 seconds
>>> sleep(.2)
0.2
>>> @timeout(.5)
... def exc():
... raise Exception('Houston we have problems!')
>>> exc()
Traceback (most recent call last):
...
Exception: Houston we have problems!
"""
import multiprocessing
import time
import logging
import traceback
logger = multiprocessing.log_to_stderr()
logger.setLevel(logging.CRITICAL)
class TimeoutException(Exception):
pass
class RunableProcessing(multiprocessing.Process):
def __init__(self, func, *args, **kwargs):
self.queue = multiprocessing.Queue(maxsize=1)
args = (func,) + args
multiprocessing.Process.__init__(self, target=self.run_func, args=args, kwargs=kwargs)
def run_func(self, func, *args, **kwargs):
try:
result = func(*args, **kwargs)
self.queue.put((True, result))
except Exception as e:
if "Boost.Python" in str(e.__class__):
e = Exception(str(e.__class__) + " " +
e.message + traceback.format_exc(e))
self.queue.put((False, e))
def done(self):
return self.queue.full()
def result(self):
return self.queue.get()
def timeout(seconds, force_kill=True):
def wrapper(function):
def inner(*args, **kwargs):
now = time.time()
proc = RunableProcessing(function, *args, **kwargs)
proc.start()
proc.join(seconds)
if proc.is_alive():
if force_kill:
proc.terminate()
runtime = int(time.time() - now)
raise TimeoutException('Function {0} timed out after {1} seconds\n args: {2}, kwargs: {3} '.format(function.func_name, runtime, args, kwargs))
assert proc.done()
success, result = proc.result()
if success:
return result
else:
raise result
return inner
return wrapper
if __name__ == '__main__':
import doctest
doctest.testmod()
## end of http://code.activestate.com/recipes/577853/ }}}