blob: f6bd43f6349acd8de931ad6b1c613667a2b47662 [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.
Serhiy Storchakae9124962012-12-29 20:57:52 +020030
Giampaolo Rodola'73520d52011-12-14 13:34:26 +010031 .. versionchanged:: 3.3
32 :class:`scheduler` class can be safely used in multi-threaded
33 environments.
Giampaolo Rodola'be55d992011-11-22 13:33:34 +010034
Georg Brandl116aa622007-08-15 14:28:22 +000035Example::
36
37 >>> import sched, time
Christian Heimesfe337bf2008-03-23 21:54:12 +000038 >>> s = sched.scheduler(time.time, time.sleep)
Georg Brandl6911e3c2007-09-04 07:15:32 +000039 >>> def print_time(): print("From print_time", time.time())
Georg Brandl116aa622007-08-15 14:28:22 +000040 ...
41 >>> def print_some_times():
Georg Brandl6911e3c2007-09-04 07:15:32 +000042 ... print(time.time())
Georg Brandl116aa622007-08-15 14:28:22 +000043 ... s.enter(5, 1, print_time, ())
44 ... s.enter(10, 1, print_time, ())
45 ... s.run()
Georg Brandl6911e3c2007-09-04 07:15:32 +000046 ... print(time.time())
Georg Brandl116aa622007-08-15 14:28:22 +000047 ...
48 >>> print_some_times()
49 930343690.257
50 From print_time 930343695.274
51 From print_time 930343700.273
52 930343700.276
53
Georg Brandl116aa622007-08-15 14:28:22 +000054.. _scheduler-objects:
55
56Scheduler Objects
57-----------------
58
Christian Heimes679db4a2008-01-18 09:56:22 +000059:class:`scheduler` instances have the following methods and attributes:
Georg Brandl116aa622007-08-15 14:28:22 +000060
61
Giampaolo Rodola'be55d992011-11-22 13:33:34 +010062.. method:: scheduler.enterabs(time, priority, action, argument=[], kwargs={})
Georg Brandl116aa622007-08-15 14:28:22 +000063
64 Schedule a new event. The *time* argument should be a numeric type compatible
65 with the return value of the *timefunc* function passed to the constructor.
66 Events scheduled for the same *time* will be executed in the order of their
67 *priority*.
68
Giampaolo Rodola'be55d992011-11-22 13:33:34 +010069 Executing the event means executing ``action(*argument, **kwargs)``.
70 *argument* must be a sequence holding the parameters for *action*.
71 *kwargs* must be a dictionary holding the keyword parameters for *action*.
Georg Brandl116aa622007-08-15 14:28:22 +000072
73 Return value is an event which may be used for later cancellation of the event
74 (see :meth:`cancel`).
75
Giampaolo Rodola'be55d992011-11-22 13:33:34 +010076 .. versionchanged:: 3.3
77 *argument* parameter is optional.
Georg Brandl116aa622007-08-15 14:28:22 +000078
Giampaolo Rodola'be55d992011-11-22 13:33:34 +010079 .. versionadded:: 3.3
80 *kwargs* parameter was added.
81
82
83.. method:: scheduler.enter(delay, priority, action, argument=[], kwargs={})
Georg Brandl116aa622007-08-15 14:28:22 +000084
Ezio Melottid3cf0db2011-10-28 12:22:25 +030085 Schedule an event for *delay* more time units. Other than the relative time, the
Georg Brandl116aa622007-08-15 14:28:22 +000086 other arguments, the effect and the return value are the same as those for
87 :meth:`enterabs`.
88
Giampaolo Rodola'be55d992011-11-22 13:33:34 +010089 .. versionchanged:: 3.3
90 *argument* parameter is optional.
91
92 .. versionadded:: 3.3
93 *kwargs* parameter was added.
Georg Brandl116aa622007-08-15 14:28:22 +000094
95.. method:: scheduler.cancel(event)
96
97 Remove the event from the queue. If *event* is not an event currently in the
Georg Brandlc38a0002009-05-26 07:51:03 +000098 queue, this method will raise a :exc:`ValueError`.
Georg Brandl116aa622007-08-15 14:28:22 +000099
100
101.. method:: scheduler.empty()
102
103 Return true if the event queue is empty.
104
105
Giampaolo Rodola'556ba042011-12-14 14:38:45 +0100106.. method:: scheduler.run(blocking=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000107
Giampaolo Rodola'556ba042011-12-14 14:38:45 +0100108 Run all scheduled events. This method will wait (using the :func:`delayfunc`
Georg Brandl116aa622007-08-15 14:28:22 +0000109 function passed to the constructor) for the next event, then execute it and so
110 on until there are no more scheduled events.
111
Giampaolo Rodola'556ba042011-12-14 14:38:45 +0100112 If *blocking* is False executes the scheduled events due to expire soonest
Giampaolo Rodola'a4e01882012-03-15 13:05:41 +0100113 (if any) and then return the deadline of the next scheduled call in the
114 scheduler (if any).
Giampaolo Rodola'556ba042011-12-14 14:38:45 +0100115
Georg Brandl116aa622007-08-15 14:28:22 +0000116 Either *action* or *delayfunc* can raise an exception. In either case, the
117 scheduler will maintain a consistent state and propagate the exception. If an
118 exception is raised by *action*, the event will not be attempted in future calls
119 to :meth:`run`.
120
121 If a sequence of events takes longer to run than the time available before the
122 next event, the scheduler will simply fall behind. No events will be dropped;
123 the calling code is responsible for canceling events which are no longer
124 pertinent.
125
Giampaolo Rodola'556ba042011-12-14 14:38:45 +0100126 .. versionadded:: 3.3
127 *blocking* parameter was added.
128
Christian Heimes679db4a2008-01-18 09:56:22 +0000129.. attribute:: scheduler.queue
130
131 Read-only attribute returning a list of upcoming events in the order they
132 will be run. Each event is shown as a :term:`named tuple` with the
Serhiy Storchakae9124962012-12-29 20:57:52 +0200133 following fields: time, priority, action, argument, kwargs.