Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +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 | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 8 | .. index:: single: event scheduling |
| 9 | |
| 10 | The :mod:`sched` module defines a class which implements a general purpose event |
| 11 | scheduler: |
| 12 | |
| 13 | |
| 14 | .. class:: scheduler(timefunc, delayfunc) |
| 15 | |
| 16 | The :class:`scheduler` class defines a generic interface to scheduling events. |
| 17 | It needs two functions to actually deal with the "outside world" --- *timefunc* |
| 18 | should be callable without arguments, and return a number (the "time", in any |
| 19 | units whatsoever). The *delayfunc* function should be callable with one |
| 20 | argument, compatible with the output of *timefunc*, and should delay that many |
| 21 | time units. *delayfunc* will also be called with the argument ``0`` after each |
| 22 | event is run to allow other threads an opportunity to run in multi-threaded |
| 23 | applications. |
| 24 | |
| 25 | Example:: |
| 26 | |
| 27 | >>> import sched, time |
| 28 | >>> s=sched.scheduler(time.time, time.sleep) |
| 29 | >>> def print_time(): print "From print_time", time.time() |
| 30 | ... |
| 31 | >>> def print_some_times(): |
| 32 | ... print time.time() |
| 33 | ... s.enter(5, 1, print_time, ()) |
| 34 | ... s.enter(10, 1, print_time, ()) |
| 35 | ... s.run() |
| 36 | ... print time.time() |
| 37 | ... |
| 38 | >>> print_some_times() |
| 39 | 930343690.257 |
| 40 | From print_time 930343695.274 |
| 41 | From print_time 930343700.273 |
| 42 | 930343700.276 |
| 43 | |
| 44 | |
| 45 | .. _scheduler-objects: |
| 46 | |
| 47 | Scheduler Objects |
| 48 | ----------------- |
| 49 | |
Raymond Hettinger | 44bd6c0 | 2008-01-17 19:31:38 +0000 | [diff] [blame^] | 50 | :class:`scheduler` instances have the following methods and attributes: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 51 | |
| 52 | |
| 53 | .. method:: scheduler.enterabs(time, priority, action, argument) |
| 54 | |
| 55 | Schedule a new event. The *time* argument should be a numeric type compatible |
| 56 | with the return value of the *timefunc* function passed to the constructor. |
| 57 | Events scheduled for the same *time* will be executed in the order of their |
| 58 | *priority*. |
| 59 | |
| 60 | Executing the event means executing ``action(*argument)``. *argument* must be a |
| 61 | sequence holding the parameters for *action*. |
| 62 | |
| 63 | Return value is an event which may be used for later cancellation of the event |
| 64 | (see :meth:`cancel`). |
| 65 | |
| 66 | |
| 67 | .. method:: scheduler.enter(delay, priority, action, argument) |
| 68 | |
| 69 | Schedule an event for *delay* more time units. Other then the relative time, the |
| 70 | other arguments, the effect and the return value are the same as those for |
| 71 | :meth:`enterabs`. |
| 72 | |
| 73 | |
| 74 | .. method:: scheduler.cancel(event) |
| 75 | |
| 76 | Remove the event from the queue. If *event* is not an event currently in the |
| 77 | queue, this method will raise a :exc:`RuntimeError`. |
| 78 | |
| 79 | |
| 80 | .. method:: scheduler.empty() |
| 81 | |
| 82 | Return true if the event queue is empty. |
| 83 | |
| 84 | |
| 85 | .. method:: scheduler.run() |
| 86 | |
| 87 | Run all scheduled events. This function will wait (using the :func:`delayfunc` |
| 88 | function passed to the constructor) for the next event, then execute it and so |
| 89 | on until there are no more scheduled events. |
| 90 | |
| 91 | Either *action* or *delayfunc* can raise an exception. In either case, the |
| 92 | scheduler will maintain a consistent state and propagate the exception. If an |
| 93 | exception is raised by *action*, the event will not be attempted in future calls |
| 94 | to :meth:`run`. |
| 95 | |
| 96 | If a sequence of events takes longer to run than the time available before the |
| 97 | next event, the scheduler will simply fall behind. No events will be dropped; |
| 98 | the calling code is responsible for canceling events which are no longer |
| 99 | pertinent. |
| 100 | |
Raymond Hettinger | 44bd6c0 | 2008-01-17 19:31:38 +0000 | [diff] [blame^] | 101 | .. attribute:: scheduler.queue |
| 102 | |
| 103 | Read-only attribute returning a list of upcoming events in the order they |
| 104 | will be run. Each event is shown as a :term:`named tuple` with the |
| 105 | following fields: time, priority, action, argument. |