[Patches] [ python-Patches-1676820 ] Add a PeriodicTimer to threading

SourceForge.net noreply at sourceforge.net
Mon Mar 12 18:40:01 CET 2007


Patches item #1676820, was opened at 2007-03-08 21:28
Message generated for change (Comment added) made by aminusfu
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1676820&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Library (Lib)
Group: None
Status: Open
Resolution: None
Priority: 5
Private: No
Submitted By: Björn Lindqvist (sonderblade)
Assigned to: Nobody/Anonymous (nobody)
Summary: Add a PeriodicTimer to threading

Initial Comment:
I have often wanted a timer in Python with the same functions as Javas Timer (http://java.sun.com/javase/6/docs/api/java/util/Timer.html). I want it repeating instead of being just a one-shot thing like Pythons Timer class. 

Here is a patch that adds such a class to the threading module. You then use it like this:

import threading

def hello():
    print "Hi there!"

t = threading.PeriodicTimer(5, hello)
t.start()    # "Hi there!" will be printed every five seconds.

The use cases for this class is things like updating canvases, updating simulations and polling stuff. I can provide documentation and unit test patches if this class is deemed worthy.


----------------------------------------------------------------------

Comment By: Robert Brewer (aminusfu)
Date: 2007-03-12 17:40

Message:
Logged In: YES 
user_id=967320
Originator: NO

Sorry about that; I meant threading._Timer.

----------------------------------------------------------------------

Comment By: Björn Lindqvist (sonderblade)
Date: 2007-03-12 10:10

Message:
Logged In: YES 
user_id=51702
Originator: YES

There is no threading._Thread class.

----------------------------------------------------------------------

Comment By: Robert Brewer (aminusfu)
Date: 2007-03-12 01:19

Message:
Logged In: YES 
user_id=967320
Originator: NO

Why not subclass threading._Thread and override just the run method? It
would make more sense, then, to stick with the "cancel" method name instead
of "end".

----------------------------------------------------------------------

Comment By: Björn Lindqvist (sonderblade)
Date: 2007-03-10 13:56

Message:
Logged In: YES 
user_id=51702
Originator: YES

"I haven't actually tried the code AssertionError is fine (although I
wonder what you get under -O)"

Actually, you get something very very weird. Try "t = Thread(); for x in
range(10): t.start()". But it is the fault of the Thread class and above my
head to fix anyhow. :)

----------------------------------------------------------------------

Comment By: Björn Lindqvist (sonderblade)
Date: 2007-03-09 22:21

Message:
Logged In: YES 
user_id=51702
Originator: YES

Here is finished patch. It includes both documentation and a few unit
tests.
File Added: add-PeriodicTimer-2.patch

----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2007-03-09 22:19

Message:
Logged In: YES 
user_id=21627
Originator: NO

I haven't actually tried the code AssertionError is fine (although I
wonder what you get under -O)

----------------------------------------------------------------------

Comment By: Björn Lindqvist (sonderblade)
Date: 2007-03-09 20:20

Message:
Logged In: YES 
user_id=51702
Originator: YES

"Also, as you can hold onto the timer, make sure invoking start/'end'
multiple times in arbitrary order gives meaningful results."

I do not understand. Is AssertionError: thread already started a
meaningful result? That is what Timer raises if you call start() on it
multiple times. The intent of PeriodicTimer is to not be reusable because I
have not seen any use case for that, but I guess I could make it so if you
think so.

----------------------------------------------------------------------

Comment By: Björn Lindqvist (sonderblade)
Date: 2007-03-09 19:40

Message:
Logged In: YES 
user_id=51702
Originator: YES

I choose the name to match the Timer class and the java.util.Timer class
in the Java API. I'll rename it to "end" so that the caller gets the hint
that further invocations of "start" is meaningless.  

----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2007-03-09 07:15

Message:
Logged In: YES 
user_id=21627
Originator: NO

Looks fine to me. The only issue I see is naming: To stop a periodic
timer, shouldn't the method name be "stop" rather than "cancel"? Also, as
you can hold onto the timer, make sure invoking start/'end' multiple times
in arbitrary order gives meaningful results.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1676820&group_id=5470


More information about the Patches mailing list