blob: ebe6c0a80c50cc2ddf8a04432d227bf089b2f555 [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
Éric Araujo29a0b572011-08-19 02:14:03 +020010**Source code:** :source:`Lib/sched.py`
11
12--------------
13
Georg Brandl8ec7f652007-08-15 14:28:01 +000014The :mod:`sched` module defines a class which implements a general purpose event
15scheduler:
16
Georg Brandl8ec7f652007-08-15 14:28:01 +000017.. class:: scheduler(timefunc, delayfunc)
18
19 The :class:`scheduler` class defines a generic interface to scheduling events.
20 It needs two functions to actually deal with the "outside world" --- *timefunc*
21 should be callable without arguments, and return a number (the "time", in any
22 units whatsoever). The *delayfunc* function should be callable with one
23 argument, compatible with the output of *timefunc*, and should delay that many
24 time units. *delayfunc* will also be called with the argument ``0`` after each
25 event is run to allow other threads an opportunity to run in multi-threaded
26 applications.
27
28Example::
29
30 >>> import sched, time
Georg Brandle8f1b002008-03-22 22:04:10 +000031 >>> s = sched.scheduler(time.time, time.sleep)
Georg Brandl8ec7f652007-08-15 14:28:01 +000032 >>> def print_time(): print "From print_time", time.time()
33 ...
34 >>> def print_some_times():
35 ... print time.time()
36 ... s.enter(5, 1, print_time, ())
37 ... s.enter(10, 1, print_time, ())
38 ... s.run()
39 ... print time.time()
40 ...
41 >>> print_some_times()
42 930343690.257
43 From print_time 930343695.274
44 From print_time 930343700.273
45 930343700.276
46
Raymond Hettingerd0ab0142008-01-17 22:27:49 +000047In multi-threaded environments, the :class:`scheduler` class has limitations
Georg Brandlc62ef8b2009-01-03 20:55:06 +000048with respect to thread-safety, inability to insert a new task before
Andrew M. Kuchling6d22c392008-01-18 02:42:52 +000049the one currently pending in a running scheduler, and holding up the main
Raymond Hettingerd0ab0142008-01-17 22:27:49 +000050thread until the event queue is empty. Instead, the preferred approach
51is to use the :class:`threading.Timer` class instead.
52
53Example::
54
55 >>> import time
56 >>> from threading import Timer
57 >>> def print_time():
58 ... print "From print_time", time.time()
59 ...
60 >>> def print_some_times():
61 ... print time.time()
62 ... Timer(5, print_time, ()).start()
Raymond Hettinger497fdbf2008-01-17 23:32:01 +000063 ... Timer(10, print_time, ()).start()
Georg Brandl7044b112009-01-03 21:04:55 +000064 ... time.sleep(11) # sleep while time-delay events execute
Georg Brandlc62ef8b2009-01-03 20:55:06 +000065 ... print time.time()
Raymond Hettingerd0ab0142008-01-17 22:27:49 +000066 ...
67 >>> print_some_times()
68 930343690.257
Raymond Hettingerd0ab0142008-01-17 22:27:49 +000069 From print_time 930343695.274
70 From print_time 930343700.273
Raymond Hettinger497fdbf2008-01-17 23:32:01 +000071 930343701.301
Raymond Hettingerd0ab0142008-01-17 22:27:49 +000072
Georg Brandl8ec7f652007-08-15 14:28:01 +000073
74.. _scheduler-objects:
75
76Scheduler Objects
77-----------------
78
Raymond Hettinger44bd6c02008-01-17 19:31:38 +000079:class:`scheduler` instances have the following methods and attributes:
Georg Brandl8ec7f652007-08-15 14:28:01 +000080
81
82.. method:: scheduler.enterabs(time, priority, action, argument)
83
84 Schedule a new event. The *time* argument should be a numeric type compatible
85 with the return value of the *timefunc* function passed to the constructor.
86 Events scheduled for the same *time* will be executed in the order of their
87 *priority*.
88
89 Executing the event means executing ``action(*argument)``. *argument* must be a
90 sequence holding the parameters for *action*.
91
92 Return value is an event which may be used for later cancellation of the event
93 (see :meth:`cancel`).
94
95
96.. method:: scheduler.enter(delay, priority, action, argument)
97
Ezio Melotti24c51802011-10-28 12:22:25 +030098 Schedule an event for *delay* more time units. Other than the relative time, the
Georg Brandl8ec7f652007-08-15 14:28:01 +000099 other arguments, the effect and the return value are the same as those for
100 :meth:`enterabs`.
101
102
103.. method:: scheduler.cancel(event)
104
105 Remove the event from the queue. If *event* is not an event currently in the
Georg Brandl4ee82822009-05-26 07:50:23 +0000106 queue, this method will raise a :exc:`ValueError`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000107
108
109.. method:: scheduler.empty()
110
111 Return true if the event queue is empty.
112
113
114.. method:: scheduler.run()
115
116 Run all scheduled events. This function will wait (using the :func:`delayfunc`
117 function passed to the constructor) for the next event, then execute it and so
118 on until there are no more scheduled events.
119
120 Either *action* or *delayfunc* can raise an exception. In either case, the
121 scheduler will maintain a consistent state and propagate the exception. If an
122 exception is raised by *action*, the event will not be attempted in future calls
123 to :meth:`run`.
124
125 If a sequence of events takes longer to run than the time available before the
126 next event, the scheduler will simply fall behind. No events will be dropped;
127 the calling code is responsible for canceling events which are no longer
128 pertinent.
129
Raymond Hettinger44bd6c02008-01-17 19:31:38 +0000130.. attribute:: scheduler.queue
131
132 Read-only attribute returning a list of upcoming events in the order they
133 will be run. Each event is shown as a :term:`named tuple` with the
134 following fields: time, priority, action, argument.
Raymond Hettinger64cd1e22008-01-17 23:56:56 +0000135
136 .. versionadded:: 2.6