Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > timers not canceling!

Reply
Thread Tools

timers not canceling!

 
 
Alex Hall
Guest
Posts: n/a
 
      03-24-2010
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.
--
Have a great day,
Alex (msg sent from GMail website)
http://www.velocityreviews.com/forums/(E-Mail Removed); http://www.facebook.com/mehgcap
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      03-24-2010
On Wed, 24 Mar 2010 17:12:45 -0400, 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



The start method is to start generic threads, not just timers, and
threads don't support being restarted.

Timers are fairly simple objects: after you start the thread, they just
wait until the time expires, then run a function. You can cancel them,
but not pause and restart them.

To do what you are trying to do, you will need to subclass either Timer
or thread and implement your own logic for pausing or restarting the
count down.


> 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.



That's not how they work. "start" means "start the thread", not "start
the timer", and threads cannot be restarted.

The documentation is very clear:

"start()
Start the thread’s activity.

It must be called at most once per thread object. It arranges for the
object’s run() method to be invoked in a separate thread of control."

http://docs.python.org/library/threa...thread-objects


--
Steven
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Timers spf R Siffredi Cisco 1 10-31-2005 05:11 PM
System.Timers.Timer/System.Threading.Timer Not Firing At All on Dev Server james.e.coleman@gmail.com ASP .Net 1 02-22-2005 09:41 PM
timers Girish Pal Singh ASP .Net 2 08-11-2003 01:00 PM
System.Timers.Timer vs. System.Threading.Timer Kelsang Wangchuk ASP .Net 0 07-31-2003 04:28 PM
Timers in application web programming Stephen Inkpen ASP .Net 1 07-16-2003 02:12 AM



Advertisments