前端之家收集整理的这篇文章主要介绍了
在Windows中超时python函数,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_
301_0@
我正在尝试为特定
功能实现超时.我已经检查过SE中的许多问题,但找不到适合我问题的任何
解决方案,因为
>我在Windows中运行python
>超时应用于我无法控制的python函数,即它在已经设计的模块中定义.
> python函数不是子进程
我正在为特定任务开发一个已经设计的自定义模块(比如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'
但我不确定哪个模块可以用于在python上实现这一点.请注意,我是一个新手,我编写的所有脚本都直接基于SE Answers或Python文档.
我认为
解决这个问题的好
方法是创建一个装饰器并使用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
然后,做这样的事情:
func = timeout(timeout=16)(MyModule.MyFunc)
try:
func()
except:
pass #handle errors here
您可以在任何需要的地方使用此装饰器,例如:
@timeout(60)
def f():
...