threading.Timer()
Question:
i have to write a program in network course that is something like selective repeat but a need a timer. after search in google i found that threading.Timer can help me, i wrote a simple program just for test how threading.Timer work that was this:
import threading
def hello():
print "hello, world"
t = threading.Timer(10.0, hello)
t.start()
print "Hi"
i=10
i=i+20
print i
this program run correctly.
but when i try to define hello function in a way that give parameter like:
import threading
def hello(s):
print s
h="hello world"
t = threading.Timer(10.0, hello(h))
t.start()
print "Hi"
i=10
i=i+20
print i
the out put is :
hello world
Hi
30
Exception in thread Thread-1:
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/threading.py", line 522, in __bootstrap_inner
self.run()
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/threading.py", line 726, in run
self.function(*self.args, **self.kwargs)
TypeError: 'NoneType' object is not callable
i cant understand what is the problem!
can any one help me?
Answers:
You just need to put the arguments to hello
into a separate item in the function call, like this,
t = threading.Timer(10.0, hello, [h])
This is a common approach in Python. Otherwise, when you use Timer(10.0, hello(h))
, the result of this function call is passed to Timer
, which is None
since hello
doesn’t make an explicit return.
An alternative is to use lambda
if you want to use normal function parameters. Basically it tells the program that the argument is a function and not to be called on assignment.
t = threading.Timer(10.0, lambda: hello(h))
i have to write a program in network course that is something like selective repeat but a need a timer. after search in google i found that threading.Timer can help me, i wrote a simple program just for test how threading.Timer work that was this:
import threading
def hello():
print "hello, world"
t = threading.Timer(10.0, hello)
t.start()
print "Hi"
i=10
i=i+20
print i
this program run correctly.
but when i try to define hello function in a way that give parameter like:
import threading
def hello(s):
print s
h="hello world"
t = threading.Timer(10.0, hello(h))
t.start()
print "Hi"
i=10
i=i+20
print i
the out put is :
hello world
Hi
30
Exception in thread Thread-1:
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/threading.py", line 522, in __bootstrap_inner
self.run()
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/threading.py", line 726, in run
self.function(*self.args, **self.kwargs)
TypeError: 'NoneType' object is not callable
i cant understand what is the problem!
can any one help me?
You just need to put the arguments to hello
into a separate item in the function call, like this,
t = threading.Timer(10.0, hello, [h])
This is a common approach in Python. Otherwise, when you use Timer(10.0, hello(h))
, the result of this function call is passed to Timer
, which is None
since hello
doesn’t make an explicit return.
An alternative is to use lambda
if you want to use normal function parameters. Basically it tells the program that the argument is a function and not to be called on assignment.
t = threading.Timer(10.0, lambda: hello(h))