Timer runs only once.

siva gnanam sivagnanam.student at gmail.com
Wed Nov 30 09:58:39 EST 2016


On Wednesday, November 30, 2016 at 7:35:46 PM UTC+5:30, siva gnanam wrote:
> The following program print hello world only once instead it has to print the string for every 5 seconds.
> 
>     from threading import Timer;
> 
>     class TestTimer:
> 
>         def __init__(self):
>             self.t1 = Timer(5.0, self.foo);
> 
>         def startTimer(self):
>             self.t1.start();
> 
>         def foo(self):
>             print("Hello, World!!!");
> 
>     timer = TestTimer();
>     timer.startTimer();
> 
> 
>                            (program - 1)
> 
> But the following program prints the string for every 5 seconds.
> 
>     def foo():
>         print("World");
>         Timer(5.0, foo).start();
> 
>     foo();
>  
>                             (program - 2)
> 
> Why (program - 1) not printing the string for every 5 seconds ?  And how to make the (program - 1) to print the string for every 5 seconds continuously.


The use case :
    Create a class which contains t1 as object variable. Assign a timer object to t1. Then make the timer running. So we can check the timer status in the future. Is it possible ?



More information about the Python-list mailing list