我有一个代码块,用于每隔30秒运行一段代码
def hello():
print "hello, world"
t = threading.Timer(30.0, hello)
t.start()
下面是一个类的方法,我真的想每30秒运行一次,但是我遇到了问题。
def continousUpdate(self, contractId):
print 'hello, world new'
t = threading.Timer(30.0, self.continousUpdate, [self, contractId],{} )
t.start()
当我运行它时,我得到以下错误
pydev debugger: starting
hello, world new
Exception in thread Thread-4:
Traceback (most recent call last):
File "C:\Python27\lib\threading.py", line 552, in __bootstrap_inner
self.run()
File "C:\Python27\lib\threading.py", line 756, in run
self.function(*self.args, **self.kwargs)
TypeError: continousUpdate() takes exactly 2 arguments (3 given)
我也试过
def continousUpdate(self, contractId):
print 'hello, world new'
t = threading.Timer(30.0, self.continousUpdate(contractId))
t.start()
它的行为好像忽略了线程,并给出了递归限制错误
试试这个:
当您读取
self.continuousUpdate
时,该方法已经绑定到对象,即使您还没有调用它。您不需要再次通过self
。第二个版本“忽略线程”的原因是您在
Timer
调用的参数中调用该方法,因此它在计时器启动之前运行(并尝试再次调用自己)。这就是线程函数让您分别传递函数及其参数的原因(这样它可以在准备好时调用函数本身)。顺便说一下,你拼错了“continuous”。
相关问题 更多 >
编程相关推荐