[Python-checkins] r60026 - python/trunk/Doc/library/sched.rst

raymond.hettinger python-checkins at python.org
Thu Jan 17 23:27:49 CET 2008


Author: raymond.hettinger
Date: Thu Jan 17 23:27:49 2008
New Revision: 60026

Modified:
   python/trunk/Doc/library/sched.rst
Log:
Add advice on choosing between scheduler and threading.Timer().

Modified: python/trunk/Doc/library/sched.rst
==============================================================================
--- python/trunk/Doc/library/sched.rst	(original)
+++ python/trunk/Doc/library/sched.rst	Thu Jan 17 23:27:49 2008
@@ -41,6 +41,31 @@
    From print_time 930343700.273
    930343700.276
 
+In multi-threaded environments, the :class:`scheduler` class has limitations
+with respect to thread-safety, inability to insert a new task before 
+the one currently pending in a running scheduler, and holding-up the main
+thread until the event queue is empty.  Instead, the preferred approach
+is to use the :class:`threading.Timer` class instead.
+
+Example::
+
+    >>> import time
+    >>> from threading import Timer
+    >>> def print_time():
+    ...     print "From print_time", time.time()
+    ...
+    >>> def print_some_times():
+    ...     print time.time()
+    ...     Timer(5, print_time, ()).start()
+    ...     Timer(10, 1, print_time, ())
+    ...     print time.time()     # executes before the time-delay events 
+    ...
+    >>> print_some_times()
+    930343690.257
+    930343690.301
+    From print_time 930343695.274
+    From print_time 930343700.273
+
 
 .. _scheduler-objects:
 


More information about the Python-checkins mailing list