我直接从网上找到的一个例子中得到了这个装饰器:
class TimedOutExc(Exception): pass def timeout(timeout): def decorate(f): def handler(signum,frame): raise TimedOutExc() def new_f(*args,**kwargs): old = signal.signal(signal.SIGALRM,handler) signal.alarm(timeout) try: result = f(*args,**kwargs) except TimedOutExc: return None finally: signal.signal(signal.SIGALRM,old) signal.alarm(0) return result new_f.func_name = f.func_name return new_f return decorate
如果f函数超时,它会抛出异常.
好吧,它可以工作但是当我在多处理函数上使用这个装饰器并且由于超时而停止时,它不会终止计算中涉及的过程.我怎样才能做到这一点?
我不想启动异常并停止程序.基本上我想要的是当f超时时,让它返回None然后终止所涉及的过程.
解决方法
虽然我同意Aaron回答的要点,但我想详细说明一下.
必须在要装饰的函数中停止多处理启动的进程;我不认为这可以通常从装饰器本身完成(装饰函数是唯一知道它启动了什么计算的实体).
您可以捕获自定义的TimedOutExc异常,而不是使装饰函数捕获SIGALARM,这可能更灵活.那么你的例子将成为:
class TimedOutExc(Exception): """ Raised when a timeout happens """ def timeout(timeout): """ Return a decorator that raises a TimedOutExc exception after timeout seconds,if the decorated function did not return. """ def decorate(f): def handler(signum,**kwargs): old_handler = signal.signal(signal.SIGALRM,handler) signal.alarm(timeout) result = f(*args,**kwargs) # f() always returns,in this scheme signal.signal(signal.SIGALRM,old_handler) # Old signal handler is restored signal.alarm(0) # Alarm removed return result new_f.func_name = f.func_name return new_f return decorate @timeout(10) def function_that_takes_a_long_time(): try: # ... long,parallel calculation ... except TimedOutExc: # ... Code that shuts down the processes ... # ... return None # Or exception raised,which means that the calculation is not complete