I have a code block that I use for running a piece of code every 30 secs
def hello():
print "hello, world"
t = threading.Timer(30.0, hello)
t.start()
The one below is a method of a class, which I really want to run every 30 secs, but I am having problems with it.
def continousUpdate(self, contractId):
print 'hello, world new'
t = threading.Timer(30.0, self.continousUpdate, [self, contractId],{} )
t.start()
When I run it, I get the following error
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)
I have also tried
def continousUpdate(self, contractId):
print 'hello, world new'
t = threading.Timer(30.0, self.continousUpdate(contractId))
t.start()
which somehow behaves as if it ignores the thread, and gives a recursion limit error
Try this:
t = threading.Timer(30.0, self.continousUpdate, [contractId],{} )
When you read self.continuousUpdate
, the method is already bound to the object, even if you don't call it yet. You don't need to pass self
again.
The reason the second version "ignores the thread" is that you call the method inside an argument to the Timer
call, so it runs (and tries to call itself again) before the Timer ever gets started. That's why threading functions have you pass the function and its arguments separately (so it can call the function itself when it's ready).
Incidentally, you spelled "continuous" wrong.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With