from threading import Timer
class test_timer():
def __init__(self):
self.awesum="hh"
self.timer = Timer(1,self.say_hello,args=["WOW"])
def say_hello(self,message):
self.awesum=message
print 'HIHIHIIHIH'
print message
raise Exception("hi")
if __name__ == '__main__':
print 'Got to main'
x=test_timer()
When I run the code above, my callback method is never triggered. I have been trying to solve this for hours but cannot figure it out >.<
To test, the timer. I run this code and check to see if x.awesum is 'WOW'
Timer is a sub class of Thread class defined in python. It is started by calling the start() function corresponding to the timer explicitly. Create a timer that will run function with arguments args and keyword arguments kwargs, after interval seconds have passed.
In Python, Timer is a subclass of Thread class. Calling the start() method, the timer starts. Timer objects are used to create some actions which are bounded by the time period. Using timer object create some threads that carries out some actions. The Timer is stopped using the cancel() method.
Python timer functions After every specified number of seconds, a timer class function is called. start() is a function that is used to initialize a timer. To end or quit the timer, one must use a cancel() function.
Python developers can create timers with the help of Python's time modules. There are two basic types of timers: timers that count up and those that count down.
You never start the timer. You need to:
self.timer.start()
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