【发布时间】:2017-01-04 18:57:31
【问题描述】:
我想在 python 中为一个函数(rest API 调用)设置一个超时时间,为此我正在关注this SO 答案。
我已经有一个现有的代码结构,我想要一个超时装饰器。我在“.txt”文件中定义超时秒数并将其作为字典传递给主函数。类似的东西:
class Foo():
def __init__(self, params):
self.timeout=params.get['timeout']
....
....
@timeout(self.timeout) #throws an error
def bar(arg1,arg2,arg3,argn):
pass
#handle timeout for this function by getting timeout from __init__
#As answer given in SO ,
#I need to use the self.timeout, which is throwing an error:
***signal.alarm(seconds)
TypeError: an integer is required***
#And also
***@timeout(self.timeout)
NameError: name 'self' is not defined***
@timeout(30) #Works fine without any issue
def foo_bar(arg1,arg2,arg3,argn):
pass
我错过了什么?
【问题讨论】:
标签: python python-2.7 function timeout settimeout