blob: 94f79915a29d65ed130c465699e3cf09d4399eb4 [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
10The :mod:`sched` module defines a class which implements a general purpose event
11scheduler:
12
Éric Araujo6e6cb8e2010-11-16 19:13:50 +000013.. seealso::
14
15 Latest version of the :source:`sched module Python source code
16 <Lib/sched.py>`
Georg Brandl116aa622007-08-15 14:28:22 +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
Christian Heimesfe337bf2008-03-23 21:54:12 +000032 >>> s = sched.scheduler(time.time, time.sleep)
Georg Brandl6911e3c2007-09-04 07:15:32 +000033 >>> def print_time(): print("From print_time", time.time())
Georg Brandl116aa622007-08-15 14:28:22 +000034 ...
35 >>> def print_some_times():
Georg Brandl6911e3c2007-09-04 07:15:32 +000036 ... print(time.time())
Georg Brandl116aa622007-08-15 14:28:22 +000037 ... s.enter(5, 1, print_time, ())
38 ... s.enter(10, 1, print_time, ())
39 ... s.run()
Georg Brandl6911e3c2007-09-04 07:15:32 +000040 ... print(time.time())
Georg Brandl116aa622007-08-15 14:28:22 +000041 ...
42 >>> print_some_times()
43 930343690.257
44 From print_time 930343695.274
45 From print_time 930343700.273
46 930343700.276
47
Christian Heimes679db4a2008-01-18 09:56:22 +000048In multi-threaded environments, the :class:`scheduler` class has limitations
Georg Brandl48310cd2009-01-03 21:18:54 +000049with respect to thread-safety, inability to insert a new task before
Christian Heimes679db4a2008-01-18 09:56:22 +000050the one currently pending in a running scheduler, and holding up the main
51thread 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():
Neal Norwitz752abd02008-05-13 04:55:24 +000059 ... print("From print_time", time.time())
Christian Heimes679db4a2008-01-18 09:56:22 +000060 ...
61 >>> def print_some_times():
Neal Norwitz752abd02008-05-13 04:55:24 +000062 ... print(time.time())
Christian Heimes679db4a2008-01-18 09:56:22 +000063 ... Timer(5, print_time, ()).start()
64 ... Timer(10, print_time, ()).start()
Georg Brandla1c6a1c2009-01-03 21:26:05 +000065 ... time.sleep(11) # sleep while time-delay events execute
Neal Norwitz752abd02008-05-13 04:55:24 +000066 ... print(time.time())
Christian Heimes679db4a2008-01-18 09:56:22 +000067 ...
68 >>> print_some_times()
69 930343690.257
70 From print_time 930343695.274
71 From print_time 930343700.273
72 930343701.301
73
Georg Brandl116aa622007-08-15 14:28:22 +000074
75.. _scheduler-objects:
76
77Scheduler Objects
78-----------------
79
Christian Heimes679db4a2008-01-18 09:56:22 +000080:class:`scheduler` instances have the following methods and attributes:
Georg Brandl116aa622007-08-15 14:28:22 +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 Brandlc38a0002009-05-26 07:51:03 +0000107 queue, this method will raise a :exc:`ValueError`.
Georg Brandl116aa622007-08-15 14:28:22 +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
Christian Heimes679db4a2008-01-18 09:56:22 +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.