Timer runs only once.

Ian Kelly ian.g.kelly at gmail.com
Wed Nov 30 14:41:15 EST 2016


On Wed, Nov 30, 2016 at 8:06 AM, siva gnanam
<sivagnanam.student at gmail.com> wrote:
> On Wednesday, November 30, 2016 at 8:11:49 PM UTC+5:30, vnthma... at gmail.com wrote:
>> from threading import Timer
>>
>> class TestTimer:
>>     def foo(self):
>>         print("hello world")
>>         self.startTimer()
>>
>>     def startTimer(self):
>>         self.t1 = Timer(5, self.foo)
>>         self.t1.start()
>>
>> timer = TestTimer()
>> timer.startTimer()
>
> I think in this example, We are creating Timer object every 5 seconds. So      every time it will span a new Timer. I don't know what happened to the previous timers we created.

Correct. Each Timer only fires once, and then the thread that it's
running in exits. After that the Timer will eventually be
garbage-collected like any other object that's no longer referenced.



More information about the Python-list mailing list