blob: 1efe47ac0d47881b1e48289788442cec711e5ed9 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001:mod:`sched` --- Event scheduler
2================================
3
4.. module:: sched
5 :synopsis: General purpose event scheduler.
6.. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
7
Georg Brandl8ec7f652007-08-15 14:28:01 +00008.. index:: single: event scheduling
9
10The :mod:`sched` module defines a class which implements a general purpose event
11scheduler:
12
Raymond Hettingere0e08222010-11-06 07:10:31 +000013.. seealso::
14
15 Latest version of the `sched module Python source code
16 <http://svn.python.org/view/python/branches/release27-maint/Lib/sched.py?view=markup>`_
Georg Brandl8ec7f652007-08-15 14:28:01 +000017
18.. class:: scheduler(timefunc, delayfunc)
19
20 The :class:`scheduler` class defines a generic interface to scheduling events.
21 It needs two functions to actually deal with the "outside world" --- *timefunc*
22 should be callable without arguments, and return a number (the "time", in any
23 units whatsoever). The *delayfunc* function should be callable with one
24 argument, compatible with the output of *timefunc*, and should delay that many
25 time units. *delayfunc* will also be called with the argument ``0`` after each
26 event is run to allow other threads an opportunity to run in multi-threaded
27 applications.
28
29Example::
30
31 >>> import sched, time
Georg Brandle8f1b002008-03-22 22:04:10 +000032 >>> s = sched.scheduler(time.time, time.sleep)
Georg Brandl8ec7f652007-08-15 14:28:01 +000033 >>> def print_time(): print "From print_time", time.time()
34 ...
35 >>> def print_some_times():
36 ... print time.time()
37 ... s.enter(5, 1, print_time, ())
38 ... s.enter(10, 1, print_time, ())
39 ... s.run()
40 ... print time.time()
41 ...
42 >>> print_some_times()
43 930343690.257
44 From print_time 930343695.274
45 From print_time 930343700.273
46 930343700.276
47
Raymond Hettingerd0ab0142008-01-17 22:27:49 +000048In multi-threaded environments, the :class:`scheduler` class has limitations
Georg Brandlc62ef8b2009-01-03 20:55:06 +000049with respect to thread-safety, inability to insert a new task before
Andrew M. Kuchling6d22c392008-01-18 02:42:52 +000050the one currently pending in a running scheduler, and holding up the main
Raymond Hettingerd0ab0142008-01-17 22:27:49 +000051thread until the event queue is empty. Instead, the preferred approach
52is to use the :class:`threading.Timer` class instead.
53
54Example::
55
56 >>> import time
57 >>> from threading import Timer
58 >>> def print_time():
59 ... print "From print_time", time.time()
60 ...
61 >>> def print_some_times():
62 ... print time.time()
63 ... Timer(5, print_time, ()).start()
Raymond Hettinger497fdbf2008-01-17 23:32:01 +000064 ... Timer(10, print_time, ()).start()
Georg Brandl7044b112009-01-03 21:04:55 +000065 ... time.sleep(11) # sleep while time-delay events execute
Georg Brandlc62ef8b2009-01-03 20:55:06 +000066 ... print time.time()
Raymond Hettingerd0ab0142008-01-17 22:27:49 +000067 ...
68 >>> print_some_times()
69 930343690.257
Raymond Hettingerd0ab0142008-01-17 22:27:49 +000070 From print_time 930343695.274
71 From print_time 930343700.273
Raymond Hettinger497fdbf2008-01-17 23:32:01 +000072 930343701.301
Raymond Hettingerd0ab0142008-01-17 22:27:49 +000073
Georg Brandl8ec7f652007-08-15 14:28:01 +000074
75.. _scheduler-objects:
76
77Scheduler Objects
78-----------------
79
Raymond Hettinger44bd6c02008-01-17 19:31:38 +000080:class:`scheduler` instances have the following methods and attributes:
Georg Brandl8ec7f652007-08-15 14:28:01 +000081
82
83.. method:: scheduler.enterabs(time, priority, action, argument)
84
85 Schedule a new event. The *time* argument should be a numeric type compatible
86 with the return value of the *timefunc* function passed to the constructor.
87 Events scheduled for the same *time* will be executed in the order of their
88 *priority*.
89
90 Executing the event means executing ``action(*argument)``. *argument* must be a
91 sequence holding the parameters for *action*.
92
93 Return value is an event which may be used for later cancellation of the event
94 (see :meth:`cancel`).
95
96
97.. method:: scheduler.enter(delay, priority, action, argument)
98
99 Schedule an event for *delay* more time units. Other then the relative time, the
100 other arguments, the effect and the return value are the same as those for
101 :meth:`enterabs`.
102
103
104.. method:: scheduler.cancel(event)
105
106 Remove the event from the queue. If *event* is not an event currently in the
Georg Brandl4ee82822009-05-26 07:50:23 +0000107 queue, this method will raise a :exc:`ValueError`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000108
109
110.. method:: scheduler.empty()
111
112 Return true if the event queue is empty.
113
114
115.. method:: scheduler.run()
116
117 Run all scheduled events. This function will wait (using the :func:`delayfunc`
118 function passed to the constructor) for the next event, then execute it and so
119 on until there are no more scheduled events.
120
121 Either *action* or *delayfunc* can raise an exception. In either case, the
122 scheduler will maintain a consistent state and propagate the exception. If an
123 exception is raised by *action*, the event will not be attempted in future calls
124 to :meth:`run`.
125
126 If a sequence of events takes longer to run than the time available before the
127 next event, the scheduler will simply fall behind. No events will be dropped;
128 the calling code is responsible for canceling events which are no longer
129 pertinent.
130
Raymond Hettinger44bd6c02008-01-17 19:31:38 +0000131.. attribute:: scheduler.queue
132
133 Read-only attribute returning a list of upcoming events in the order they
134 will be run. Each event is shown as a :term:`named tuple` with the
135 following fields: time, priority, action, argument.
Raymond Hettinger64cd1e22008-01-17 23:56:56 +0000136
137 .. versionadded:: 2.6