Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Timer Callback Method

Tags:

python

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'

like image 239
user1431282 Avatar asked Nov 12 '12 04:11

user1431282


People also ask

How do you make a timer event in Python?

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.

What is timer method in Python?

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.

How do you start and end a timer in Python?

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.

Can you make a timer in Python?

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.


1 Answers

You never start the timer. You need to:

self.timer.start()
like image 140
Tim Avatar answered Oct 01 '22 03:10

Tim