我想在一些特定的功能上做个装饰。如果函数工作超过,比如说5秒,deco将引发错误或中断函数。你知道吗
def time_limit(func):
def time_out():
raise AssertionError
@wraps(func)
def deco(*args, **kwargs):
timer = Timer(5, time_out)
timer.start()
res = func(*args, **kwargs)
return res
return deco
然而,即使装饰工作,功能仍然工作没有中断:
In [69]: @time_limit
...: def f():
...: time.sleep(6)
...: print 'aaa'
...:
In [70]: f()
Exception in thread Thread-2764:
Traceback (most recent call last):
File "/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
File "/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 1073, in run
self.function(*self.args, **self.kwargs)
File "<ipython-input-68-cc74c901d8b8>", line 4, in time_out
raise AssertionError
AssertionError
aaa
如何解决这个问题? 另外,我正在使用apscheduler循环函数,所以超时装饰更好。你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐