msg52114 - (view) |
Author: Björn Lindqvist (sonderblade) |
Date: 2007-03-08 21:28 |
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. |
|
|
msg52115 - (view) |
Author: Martin v. Löwis (loewis) *  |
Date: 2007-03-09 07:15 |
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. |
|
|
msg52116 - (view) |
Author: Björn Lindqvist (sonderblade) |
Date: 2007-03-09 19:40 |
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. |
|
|
msg52117 - (view) |
Author: Björn Lindqvist (sonderblade) |
Date: 2007-03-09 20:20 |
"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. |
|
|
msg52118 - (view) |
Author: Martin v. Löwis (loewis) *  |
Date: 2007-03-09 22:19 |
I haven't actually tried the code AssertionError is fine (although I wonder what you get under -O) |
|
|
msg52119 - (view) |
Author: Björn Lindqvist (sonderblade) |
Date: 2007-03-09 22:21 |
Here is finished patch. It includes both documentation and a few unit tests. File Added: add-PeriodicTimer-2.patch |
|
|
msg52120 - (view) |
Author: Björn Lindqvist (sonderblade) |
Date: 2007-03-10 13:56 |
"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. :) |
|
|
msg52121 - (view) |
Author: Robert Brewer (aminusfu) |
Date: 2007-03-12 01:19 |
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". |
|
|
msg52122 - (view) |
Author: Björn Lindqvist (sonderblade) |
Date: 2007-03-12 10:10 |
There is no threading._Thread class. |
|
|
msg52123 - (view) |
Author: Robert Brewer (aminusfu) |
Date: 2007-03-12 17:40 |
Sorry about that; I meant threading._Timer. |
|
|
msg52124 - (view) |
Author: Björn Lindqvist (sonderblade) |
Date: 2007-03-12 21:11 |
Pretty pointless I think, the semantics for the two classes are totally different. The method is named end() because it has a very different meaning than the Timer class' cancel(). |
|
|
msg110529 - (view) |
Author: Mark Lawrence (BreamoreBoy) * |
Date: 2010-07-16 23:52 |
"Looks fine to me". The code change is small, the rest of the patch is doc changes or unit tests, could someone run with this? |
|
|
msg116624 - (view) |
Author: Mark Lawrence (BreamoreBoy) * |
Date: 2010-09-16 22:04 |
Could someone with commit privileges please take a look at the patch, thanks. |
|
|
msg116635 - (view) |
Author: Antoine Pitrou (pitrou) *  |
Date: 2010-09-16 22:58 |
I don't really agree with "negative periods behave like if the period was 0". Negative periods don't mean anything and should just raise ValueError. (as for 0, well, we can allow it anyway, although it means the timer just degenerates into an busy loop) The doc will have to be converted to the new Sphinx format, as well. |
|
|
msg116640 - (view) |
Author: Jeffrey Yasskin (jyasskin) *  |
Date: 2010-09-16 23:42 |
Java's Timer class tends to be discouraged these days in favor of ScheduledExecutorService.scheduleAtFixedRate and .scheduleWithFixedDelay (http://download.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledExecutorService.html). Could you say why you modeled this interface off of java.util.Timer instead of ScheduledExecutor? See http://www.python.org/dev/peps/pep-3148/ for the basis of executors in Python. I'm skeptical of having a PeriodicTimer that embeds a fixed-delay timing. Both fixed-rate and fixed-delay are useful for different things, and given the ambiguity we probably shouldn't give in to the temptation to guess. I'm skeptical of having a new thread per periodic task. It's a little more work to multiplex tasks onto shared threads, and maybe that work isn't worth it for now, but I think the interface should allow future implementations to multiplex their tasks. |
|
|
msg381040 - (view) |
Author: Irit Katriel (iritkatriel) *  |
Date: 2020-11-15 22:55 |
Based on the comments, should this be closed as rejected? |
|
|
msg391248 - (view) |
Author: Irit Katriel (iritkatriel) *  |
Date: 2021-04-16 18:43 |
I'm closing this as rejected because the comments were not very supportive of the idea and there has been no activity for over 10 years. There are also more possibilities for this now with asyncio. |
|
|