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 | from functools import wraps import time import signal import errno class TimeoutError(Exception): pass def funcTimeout(timeout=30): def decorator(func): def handleFuncTimeout(signum, frame): raise TimeoutError(os.strerror(errno.ETIME)) @wraps(func) def wrapper(*args, **kwargs): #print '[%d]wrapper START' % id sTime = time.time() signal.signal(signal.SIGALRM, handleFuncTimeout) signal.alarm(timeout) try: result = func(*args, **kwargs) finally: signal.alarm(0) #print '[%d]wrapper END(elapseTime=%d)' % (id, time.time()-sTime) return result return wrapper return decorator #DIRECTIONS @funcTimeout(3) def func(): print 'func() start' time.sleep(5) print 'func() end' func() |
python function timeout decorator
it's function's timeout decorator on python
피드 구독하기:
댓글 (Atom)
댓글 없음:
댓글 쓰기