pun*_*vak 9 python timeout python-2.7
我正在尝试为特定功能实现超时.我已经检查过SE中的许多问题,但找不到适合我问题的任何解决方案,因为
我正在为特定任务开发一个已经设计的自定义模块(比如MyModule),并且在其中定义了函数.其中一个函数(比如MyFunc)由于外部因素而倾向于永远运行,我只是不希望python脚本挂起.
我打算添加一个超时功能,如下面的伪代码所示
import MyModule
set_timeout(T)
MyResult=MyModule.MyFunc()
#Come to this part of script after execution of MyFunc() or after T seconds (the latter on priority)
if Timeout occurred:
print 'MyFunc did not execute completely'
else:
print 'MyFunc completed'
Run Code Online (Sandbox Code Playgroud)
但我不确定哪个模块可用于在python上实现这一点.请注意,我是一个新手,我编写的所有脚本都直接基于SE Answers或Python文档.
acu*_*ner 25
我认为解决这个问题的好方法是创建一个装饰器并使用Thread.join(timeout)方法.请记住,杀死线程没有好办法,所以只要你的程序正在运行,它就会或多或少地继续在后台运行.
首先,创建一个这样的装饰器:
from threading import Thread
import functools
def timeout(timeout):
def deco(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
res = [Exception('function [%s] timeout [%s seconds] exceeded!' % (func.__name__, timeout))]
def newFunc():
try:
res[0] = func(*args, **kwargs)
except Exception, e:
res[0] = e
t = Thread(target=newFunc)
t.daemon = True
try:
t.start()
t.join(timeout)
except Exception, je:
print 'error starting thread'
raise je
ret = res[0]
if isinstance(ret, BaseException):
raise ret
return ret
return wrapper
return deco
Run Code Online (Sandbox Code Playgroud)
然后,做这样的事情:
func = timeout(timeout=16)(MyModule.MyFunc)
try:
func()
except:
pass #handle errors here
Run Code Online (Sandbox Code Playgroud)
您可以在任何需要的地方使用此装饰器,例如:
@timeout(60)
def f():
...
Run Code Online (Sandbox Code Playgroud)
@ acushner的答案适用于python 3.5:
from threading import Thread
import functools
def timeout(seconds_before_timeout):
def deco(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
res = [Exception('function [%s] timeout [%s seconds] exceeded!' % (func.__name__, seconds_before_timeout))]
def newFunc():
try:
res[0] = func(*args, **kwargs)
except Exception as e:
res[0] = e
t = Thread(target=newFunc)
t.daemon = True
try:
t.start()
t.join(seconds_before_timeout)
except Exception as e:
print('error starting thread')
raise e
ret = res[0]
if isinstance(ret, BaseException):
raise ret
return ret
return wrapper
return deco
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9137 次 |
| 最近记录: |