-
-
Save aorcsik/bcc17a299434ee2a2a1a to your computer and use it in GitHub Desktop.
My experiments to create a timeout decorator in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import wraps | |
from multiprocessing import TimeoutError, Queue, Process | |
from multiprocessing.pool import ThreadPool | |
import threading | |
import weakref | |
import thread | |
import signal | |
from time import sleep | |
from random import random | |
def signal_timeout(timeout): | |
def wrap_function(func): | |
@wraps(func) | |
def __wrapper(*args, **kwargs): | |
def handler(signum, frame): | |
raise TimeoutError() | |
old = signal.signal(signal.SIGALRM, handler) | |
signal.setitimer(signal.ITIMER_REAL, float(timeout) / 1000) | |
try: | |
result = func(*args, **kwargs) | |
finally: | |
signal.signal(signal.SIGALRM, old) | |
return result | |
return __wrapper | |
return wrap_function | |
def process_timeout(timeout): | |
def wrap_function(func): | |
@wraps(func) | |
def __wrapper(*args, **kwargs): | |
def queue_wrapper(args, kwargs): | |
q.put(func(*args, **kwargs)) | |
q = Queue() | |
p = Process(target=queue_wrapper, args=(args, kwargs)) | |
p.start() | |
p.join(float(timeout) / 1000) | |
if p.is_alive(): | |
p.terminate() | |
p.join() | |
raise TimeoutError() | |
p.terminate() | |
return q.get() | |
return __wrapper | |
return wrap_function | |
thread_pool = None | |
def get_thread_pool(): | |
global thread_pool | |
if thread_pool is None: | |
# fix for python <2.7.2 | |
if not hasattr(threading.current_thread(), "_children"): | |
threading.current_thread()._children = weakref.WeakKeyDictionary() | |
thread_pool = ThreadPool(processes=1) | |
return thread_pool | |
def thread_timeout(timeout): | |
def wrap_function(func): | |
@wraps(func) | |
def __wrapper(*args, **kwargs): | |
try: | |
async_result = get_thread_pool().apply_async(func, args=args, kwds=kwargs) | |
return async_result.get(float(timeout) / 1000) | |
except thread.error: | |
return func(*args, **kwargs) | |
return __wrapper | |
return wrap_function | |
@signal_timeout(500) | |
def test_a(t, message): | |
sleep(t) | |
return message | |
@process_timeout(500) | |
def test_b(t, message): | |
sleep(t) | |
return message | |
@thread_timeout(500) | |
def test_c(t, message): | |
sleep(t) | |
return message | |
if __name__ == "__main__": | |
def a(t, message): | |
try: | |
return test_a(t, message) | |
except TimeoutError: | |
return "Timeout!" | |
def b(t, message): | |
try: | |
return test_b(t, message) | |
except TimeoutError: | |
return "Timeout!" | |
def c(t, message): | |
try: | |
return test_c(t, message) | |
except TimeoutError: | |
return "Timeout!" | |
print 1, a(0.4, "Yeeey!") | |
print 2, a(10, "Booooooo!") | |
print 3, b(0.4, "Yeeey!") | |
print 4, b(10, "Booooooo!") | |
print 5, c(0.4, "Yeeey!") | |
print 6, c(10, "Booooooo!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
however
test_c
is single thread and can not execute in the same time with several threads..