Patches item #1676820, was opened at 2007-03-08 22:28 Message generated for change (Comment added) made by sonderblade 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: Bj�rn Lindqvist (sonderblade) Date: 2007-03-10 14: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 23: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 23: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 21: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 20: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 08: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
_______________________________________________ Patches mailing list Patches@python.org http://mail.python.org/mailman/listinfo/patches