[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Re: timers not canceling!

Steve Holden

3/24/2010 9:36:00 PM

Alex Hall wrote:
> Hi all,
> I am having trouble with a timer I am trying to use. It is the same
> timer, but I need to cancel it when a certain event happens, then
> start it again when a second event happens. The below is from a shell
> session, not a file, but it shows my problem: I call cancel on a
> timer, then call start on it, and it thinks it is already running?
> What am I missing?
>
>>>> from threading import Timer
>>>> def func():
> ... print("Time up!")
> ...
>>>> t=Timer(10.0, func)
>>>> t.start()
>>>> t.cancel()
>>>> t.start()
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "C:\python26\lib\threading.py", line 465, in start
> raise RuntimeError("thread already started")
> RuntimeError: thread already started
>
> I typed start, then typed cancel within ten seconds (probably four or
> five), then called start again a couple seconds later. I figured
> canceling the timer would kill the thread so I could start it again. I
> am not looking for a reset, since I do not want it counting always.
> Thanks.

I believe you'll need to create a new Timer each time you need one - a
Timer is a threading.thread, and these can only be started once.

"thread already started" implies that the thread is running, but you
actually get the same message if you try to start any terminated thread
(including a canceled one), so "threads cannot be restarted" might be a
better message.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
See PyCon Talks from Atlanta 2010 http://pyco...
Holden Web LLC http://www.hold...
UPCOMING EVENTS: http://holdenweb.event...