blob: a644ed2a69ec61d8e4b25c3ba20fa5554920f690 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +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 Brandl116aa622007-08-15 14:28:22 +00008.. index:: single: event scheduling
9
Raymond Hettinger10480942011-01-10 03:26:08 +000010**Source code:** :source:`Lib/sched.py`
11
Raymond Hettinger4f707fd2011-01-10 19:54:11 +000012--------------
13
Georg Brandl116aa622007-08-15 14:28:22 +000014The :mod:`sched` module defines a class which implements a general purpose event
15scheduler:
16
Giampaolo Rodola'be55d992011-11-22 13:33:34 +010017.. class:: scheduler(timefunc=time.time, delayfunc=time.sleep)
Georg Brandl116aa622007-08-15 14:28:22 +000018
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
Giampaolo Rodola'be55d992011-11-22 13:33:34 +010028 .. versionchanged:: 3.3
29 *timefunc* and *delayfunc* parameters are optional.
30
Georg Brandl116aa622007-08-15 14:28:22 +000031Example::
32
33 >>> import sched, time
Christian Heimesfe337bf2008-03-23 21:54:12 +000034 >>> s = sched.scheduler(time.time, time.sleep)
Georg Brandl6911e3c2007-09-04 07:15:32 +000035 >>> def print_time(): print("From print_time", time.time())
Georg Brandl116aa622007-08-15 14:28:22 +000036 ...
37 >>> def print_some_times():
Georg Brandl6911e3c2007-09-04 07:15:32 +000038 ... print(time.time())
Georg Brandl116aa622007-08-15 14:28:22 +000039 ... s.enter(5, 1, print_time, ())
40 ... s.enter(10, 1, print_time, ())
41 ... s.run()
Georg Brandl6911e3c2007-09-04 07:15:32 +000042 ... print(time.time())
Georg Brandl116aa622007-08-15 14:28:22 +000043 ...
44 >>> print_some_times()
45 930343690.257
46 From print_time 930343695.274
47 From print_time 930343700.273
48 930343700.276
49
Christian Heimes679db4a2008-01-18 09:56:22 +000050In multi-threaded environments, the :class:`scheduler` class has limitations
Georg Brandl48310cd2009-01-03 21:18:54 +000051with respect to thread-safety, inability to insert a new task before
Christian Heimes679db4a2008-01-18 09:56:22 +000052the one currently pending in a running scheduler, and holding up the main
53thread until the event queue is empty. Instead, the preferred approach
54is to use the :class:`threading.Timer` class instead.
55
56Example::
57
58 >>> import time
59 >>> from threading import Timer
60 >>> def print_time():
Neal Norwitz752abd02008-05-13 04:55:24 +000061 ... print("From print_time", time.time())
Christian Heimes679db4a2008-01-18 09:56:22 +000062 ...
63 >>> def print_some_times():
Neal Norwitz752abd02008-05-13 04:55:24 +000064 ... print(time.time())
Christian Heimes679db4a2008-01-18 09:56:22 +000065 ... Timer(5, print_time, ()).start()
66 ... Timer(10, print_time, ()).start()
Georg Brandla1c6a1c2009-01-03 21:26:05 +000067 ... time.sleep(11) # sleep while time-delay events execute
Neal Norwitz752abd02008-05-13 04:55:24 +000068 ... print(time.time())
Christian Heimes679db4a2008-01-18 09:56:22 +000069 ...
70 >>> print_some_times()
71 930343690.257
72 From print_time 930343695.274
73 From print_time 930343700.273
74 930343701.301
75
Georg Brandl116aa622007-08-15 14:28:22 +000076
77.. _scheduler-objects:
78
79Scheduler Objects
80-----------------
81
Christian Heimes679db4a2008-01-18 09:56:22 +000082:class:`scheduler` instances have the following methods and attributes:
Georg Brandl116aa622007-08-15 14:28:22 +000083
84
Giampaolo Rodola'be55d992011-11-22 13:33:34 +010085.. method:: scheduler.enterabs(time, priority, action, argument=[], kwargs={})
Georg Brandl116aa622007-08-15 14:28:22 +000086
87 Schedule a new event. The *time* argument should be a numeric type compatible
88 with the return value of the *timefunc* function passed to the constructor.
89 Events scheduled for the same *time* will be executed in the order of their
90 *priority*.
91
Giampaolo Rodola'be55d992011-11-22 13:33:34 +010092 Executing the event means executing ``action(*argument, **kwargs)``.
93 *argument* must be a sequence holding the parameters for *action*.
94 *kwargs* must be a dictionary holding the keyword parameters for *action*.
Georg Brandl116aa622007-08-15 14:28:22 +000095
96 Return value is an event which may be used for later cancellation of the event
97 (see :meth:`cancel`).
98
Giampaolo Rodola'be55d992011-11-22 13:33:34 +010099 .. versionchanged:: 3.3
100 *argument* parameter is optional.
Georg Brandl116aa622007-08-15 14:28:22 +0000101
Giampaolo Rodola'be55d992011-11-22 13:33:34 +0100102 .. versionadded:: 3.3
103 *kwargs* parameter was added.
104
105
106.. method:: scheduler.enter(delay, priority, action, argument=[], kwargs={})
Georg Brandl116aa622007-08-15 14:28:22 +0000107
Ezio Melottid3cf0db2011-10-28 12:22:25 +0300108 Schedule an event for *delay* more time units. Other than the relative time, the
Georg Brandl116aa622007-08-15 14:28:22 +0000109 other arguments, the effect and the return value are the same as those for
110 :meth:`enterabs`.
111
Giampaolo Rodola'be55d992011-11-22 13:33:34 +0100112 .. versionchanged:: 3.3
113 *argument* parameter is optional.
114
115 .. versionadded:: 3.3
116 *kwargs* parameter was added.
Georg Brandl116aa622007-08-15 14:28:22 +0000117
118.. method:: scheduler.cancel(event)
119
120 Remove the event from the queue. If *event* is not an event currently in the
Georg Brandlc38a0002009-05-26 07:51:03 +0000121 queue, this method will raise a :exc:`ValueError`.
Georg Brandl116aa622007-08-15 14:28:22 +0000122
123
124.. method:: scheduler.empty()
125
126 Return true if the event queue is empty.
127
128
129.. method:: scheduler.run()
130
131 Run all scheduled events. This function will wait (using the :func:`delayfunc`
132 function passed to the constructor) for the next event, then execute it and so
133 on until there are no more scheduled events.
134
135 Either *action* or *delayfunc* can raise an exception. In either case, the
136 scheduler will maintain a consistent state and propagate the exception. If an
137 exception is raised by *action*, the event will not be attempted in future calls
138 to :meth:`run`.
139
140 If a sequence of events takes longer to run than the time available before the
141 next event, the scheduler will simply fall behind. No events will be dropped;
142 the calling code is responsible for canceling events which are no longer
143 pertinent.
144
Christian Heimes679db4a2008-01-18 09:56:22 +0000145.. attribute:: scheduler.queue
146
147 Read-only attribute returning a list of upcoming events in the order they
148 will be run. Each event is shown as a :term:`named tuple` with the
149 following fields: time, priority, action, argument.