Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 44f8696

Browse files
committed
Patch #428326: New class threading.Timer.
1 parent b3a639e commit 44f8696

3 files changed

Lines changed: 69 additions & 1 deletion

File tree

Doc/lib/libthreading.tex

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ \section{\module{threading} ---
8282
A class that represents a thread of control. This class can be safely subclassed in a limited fashion.
8383
\end{classdesc*}
8484

85+
\begin{classdesc*}{Timer}{}
86+
A thread that executes a function after a specified interval has passed.
87+
\end{classdesc*}
88+
8589
Detailed interfaces for the objects are documented below.
8690

8791
The design of this module is loosely based on Java's threading model.
@@ -595,3 +599,35 @@ \subsection{Thread Objects \label{thread-objects}}
595599
The entire Python program exits when no active non-daemon
596600
threads are left.
597601
\end{methoddesc}
602+
603+
604+
\subsection{Timer Objects \label{timer-objects}}
605+
606+
This class represents an action that should be run only after a certain amount
607+
of time has passed --- a timer. \class{Timer} is a subclass of \class{Thread} and
608+
as such also functions as an example of creating custom threads.
609+
610+
Timers are started, as with threads, by calling their \method{start()} method. The
611+
timer can be stopped (before its action has begun) by calling the
612+
\method{cancel()} method. The interval the timer will wait before executing
613+
its action may not be exactly the same as the interval specified by the
614+
user.
615+
616+
For example:
617+
\begin{verbatim}
618+
def hello():
619+
print "hello, world"
620+
621+
t = Timer(30.0, hello)
622+
t.start() # after 30 seconds, "hello, world" will be printed
623+
\end{verbatim}
624+
625+
\begin{classdesc}{Timer}{interval, function, args=[], kwargs=\{\}}
626+
Create a timer that will run \var{function} with arguments \var{args} and
627+
keyword arguments \var{kwargs}, after \var{interval} seconds have passed.
628+
\end{classdesc}
629+
630+
\begin{methoddesc}{cancel}{}
631+
Stop the timer, and cancel the execution of the timer's action. This will only
632+
work if the timer is still in its waiting stage.
633+
\end{methoddesc}

Lib/threading.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,6 @@ def wait(self, timeout=None):
331331
self.__cond.wait(timeout)
332332
self.__cond.release()
333333

334-
335334
# Helper to generate new thread names
336335
_counter = 0
337336
def _newname(template="Thread-%d"):
@@ -483,6 +482,36 @@ def setDaemon(self, daemonic):
483482
assert not self.__started, "cannot set daemon status of active thread"
484483
self.__daemonic = daemonic
485484

485+
# The timer class was contributed by Itamar Shtull-Trauring
486+
487+
def Timer(*args, **kwargs):
488+
return _Timer(*args, **kwargs)
489+
490+
class _Timer(Thread):
491+
"""Call a function after a specified number of seconds:
492+
493+
t = Timer(30.0, f, args=[], kwargs={})
494+
t.start()
495+
t.cancel() # stop the timer's action if it's still waiting
496+
"""
497+
498+
def __init__(self, interval, function, args=[], kwargs={}):
499+
Thread.__init__(self)
500+
self.interval = interval
501+
self.function = function
502+
self.args = args
503+
self.kwargs = kwargs
504+
self.finished = Event()
505+
506+
def cancel(self):
507+
"""Stop the timer if it hasn't finished yet"""
508+
self.finished.set()
509+
510+
def run(self):
511+
self.finished.wait(self.interval)
512+
if not self.finished.isSet():
513+
self.function(*self.args, **self.kwargs)
514+
self.finished.set()
486515

487516
# Special thread class to represent the main thread
488517
# This is garbage collected through an exit handler

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ Core
8181

8282
Library
8383

84+
- Asynchronous timeout actions are available through the new class
85+
threading.Timer.
86+
8487
- math.log and math.log10 now return sensible results for even huge
8588
long arguments. For example, math.log10(10 ** 10000) ~= 10000.0.
8689

0 commit comments

Comments
 (0)