As others have noted, the error is that you are not passing the correct arguments to the threading.Timer() method. A fix that will run your function once, after 5 seconds. There are several ways to make it repeat.
The
object-oriented approach should be to get a new subclass of
threading.Thread . Although you could create one that does exactly what you want - namely
print "%s" % hello - itβs a little harder to create a more general, parameterized subclass that will call the function passed to it at the time of its creation (just like
threading.Timer() ). This is shown below:
import threading import time class RepeatEvery(threading.Thread): def __init__(self, interval, func, *args, **kwargs): threading.Thread.__init__(self) self.interval = interval
Output:
# starting
In addition to overriding the basic methods of threading.Thread class __init__() and run() , a stop() method has been added, which allows you to stop the thread if desired. I also simplified print "%s" % hello in your greeting() function only print hello .
source share