Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 8 | .. index:: single: event scheduling |
| 9 | |
Raymond Hettinger | 1048094 | 2011-01-10 03:26:08 +0000 | [diff] [blame] | 10 | **Source code:** :source:`Lib/sched.py` |
| 11 | |
Raymond Hettinger | 4f707fd | 2011-01-10 19:54:11 +0000 | [diff] [blame] | 12 | -------------- |
| 13 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 14 | The :mod:`sched` module defines a class which implements a general purpose event |
| 15 | scheduler: |
| 16 | |
Giampaolo Rodola' | be55d99 | 2011-11-22 13:33:34 +0100 | [diff] [blame] | 17 | .. class:: scheduler(timefunc=time.time, delayfunc=time.sleep) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 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 | |
Giampaolo Rodola' | be55d99 | 2011-11-22 13:33:34 +0100 | [diff] [blame] | 28 | .. versionchanged:: 3.3 |
| 29 | *timefunc* and *delayfunc* parameters are optional. |
Giampaolo Rodola' | 73520d5 | 2011-12-14 13:34:26 +0100 | [diff] [blame^] | 30 | .. versionchanged:: 3.3 |
| 31 | :class:`scheduler` class can be safely used in multi-threaded |
| 32 | environments. |
Giampaolo Rodola' | be55d99 | 2011-11-22 13:33:34 +0100 | [diff] [blame] | 33 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 34 | Example:: |
| 35 | |
| 36 | >>> import sched, time |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 37 | >>> s = sched.scheduler(time.time, time.sleep) |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 38 | >>> def print_time(): print("From print_time", time.time()) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 39 | ... |
| 40 | >>> def print_some_times(): |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 41 | ... print(time.time()) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 42 | ... s.enter(5, 1, print_time, ()) |
| 43 | ... s.enter(10, 1, print_time, ()) |
| 44 | ... s.run() |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 45 | ... print(time.time()) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 46 | ... |
| 47 | >>> print_some_times() |
| 48 | 930343690.257 |
| 49 | From print_time 930343695.274 |
| 50 | From print_time 930343700.273 |
| 51 | 930343700.276 |
| 52 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 53 | .. _scheduler-objects: |
| 54 | |
| 55 | Scheduler Objects |
| 56 | ----------------- |
| 57 | |
Christian Heimes | 679db4a | 2008-01-18 09:56:22 +0000 | [diff] [blame] | 58 | :class:`scheduler` instances have the following methods and attributes: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 59 | |
| 60 | |
Giampaolo Rodola' | be55d99 | 2011-11-22 13:33:34 +0100 | [diff] [blame] | 61 | .. method:: scheduler.enterabs(time, priority, action, argument=[], kwargs={}) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 62 | |
| 63 | Schedule a new event. The *time* argument should be a numeric type compatible |
| 64 | with the return value of the *timefunc* function passed to the constructor. |
| 65 | Events scheduled for the same *time* will be executed in the order of their |
| 66 | *priority*. |
| 67 | |
Giampaolo Rodola' | be55d99 | 2011-11-22 13:33:34 +0100 | [diff] [blame] | 68 | Executing the event means executing ``action(*argument, **kwargs)``. |
| 69 | *argument* must be a sequence holding the parameters for *action*. |
| 70 | *kwargs* must be a dictionary holding the keyword parameters for *action*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 71 | |
| 72 | Return value is an event which may be used for later cancellation of the event |
| 73 | (see :meth:`cancel`). |
| 74 | |
Giampaolo Rodola' | be55d99 | 2011-11-22 13:33:34 +0100 | [diff] [blame] | 75 | .. versionchanged:: 3.3 |
| 76 | *argument* parameter is optional. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 77 | |
Giampaolo Rodola' | be55d99 | 2011-11-22 13:33:34 +0100 | [diff] [blame] | 78 | .. versionadded:: 3.3 |
| 79 | *kwargs* parameter was added. |
| 80 | |
| 81 | |
| 82 | .. method:: scheduler.enter(delay, priority, action, argument=[], kwargs={}) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 83 | |
Ezio Melotti | d3cf0db | 2011-10-28 12:22:25 +0300 | [diff] [blame] | 84 | Schedule an event for *delay* more time units. Other than the relative time, the |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 85 | other arguments, the effect and the return value are the same as those for |
| 86 | :meth:`enterabs`. |
| 87 | |
Giampaolo Rodola' | be55d99 | 2011-11-22 13:33:34 +0100 | [diff] [blame] | 88 | .. versionchanged:: 3.3 |
| 89 | *argument* parameter is optional. |
| 90 | |
| 91 | .. versionadded:: 3.3 |
| 92 | *kwargs* parameter was added. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 93 | |
| 94 | .. method:: scheduler.cancel(event) |
| 95 | |
| 96 | Remove the event from the queue. If *event* is not an event currently in the |
Georg Brandl | c38a000 | 2009-05-26 07:51:03 +0000 | [diff] [blame] | 97 | queue, this method will raise a :exc:`ValueError`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 98 | |
| 99 | |
| 100 | .. method:: scheduler.empty() |
| 101 | |
| 102 | Return true if the event queue is empty. |
| 103 | |
| 104 | |
| 105 | .. method:: scheduler.run() |
| 106 | |
| 107 | Run all scheduled events. This function will wait (using the :func:`delayfunc` |
| 108 | function passed to the constructor) for the next event, then execute it and so |
| 109 | on until there are no more scheduled events. |
| 110 | |
| 111 | Either *action* or *delayfunc* can raise an exception. In either case, the |
| 112 | scheduler will maintain a consistent state and propagate the exception. If an |
| 113 | exception is raised by *action*, the event will not be attempted in future calls |
| 114 | to :meth:`run`. |
| 115 | |
| 116 | If a sequence of events takes longer to run than the time available before the |
| 117 | next event, the scheduler will simply fall behind. No events will be dropped; |
| 118 | the calling code is responsible for canceling events which are no longer |
| 119 | pertinent. |
| 120 | |
Christian Heimes | 679db4a | 2008-01-18 09:56:22 +0000 | [diff] [blame] | 121 | .. attribute:: scheduler.queue |
| 122 | |
| 123 | Read-only attribute returning a list of upcoming events in the order they |
| 124 | will be run. Each event is shown as a :term:`named tuple` with the |
| 125 | following fields: time, priority, action, argument. |