classification
Title: Add a PeriodicTimer to threading
Type: enhancement Stage: patch review
Components: Library (Lib) Versions: Python 3.2
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: aminusfu, jyasskin, loewis, pitrou, sonderblade
Priority: normal Keywords: patch

Created on 2007-03-08 21:28 by sonderblade, last changed 2014-02-03 19:43 by BreamoreBoy.

Files
File name Uploaded Description Edit
add-PeriodicTimer.patch sonderblade, 2007-03-08 21:28 review
add-PeriodicTimer-2.patch sonderblade, 2007-03-09 22:21 review
Messages (15)
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) * (Python committer) 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) * (Python committer) 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
msg52115 "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) * (Python committer) 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) * (Python committer) 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.
History
Date User Action Args
2014-02-03 19:43:47BreamoreBoysetnosy: - BreamoreBoy
2010-09-16 23:42:09jyasskinsetmessages: + msg116640
2010-09-16 22:58:19pitrousetnosy: + jyasskin
messages: + msg116635
2010-09-16 22:06:31eric.araujosetnosy: + pitrou
2010-09-16 22:04:06BreamoreBoysetmessages: + msg116624
2010-07-16 23:52:31BreamoreBoysetnosy: + BreamoreBoy

messages: + msg110529
versions: + Python 3.2, - Python 3.1, Python 2.7
2009-03-30 22:09:28ajaksu2setstage: patch review
versions: + Python 3.1, Python 2.7, - Python 2.6, Python 3.0
2008-01-05 20:12:42christian.heimessettype: enhancement
versions: + Python 2.6, Python 3.0
2007-03-08 21:28:21sonderbladecreate