Guido van Rossum | 4b8c6ea | 2000-02-04 15:39:30 +0000 | [diff] [blame] | 1 | """A generally useful event scheduler class. |
Guido van Rossum | 2d844d1 | 1991-04-07 13:41:50 +0000 | [diff] [blame] | 2 | |
Fred Drake | 5c4012a | 1999-06-25 18:53:23 +0000 | [diff] [blame] | 3 | Each instance of this class manages its own queue. |
| 4 | No multi-threading is implied; you are supposed to hack that |
| 5 | yourself, or use a single instance per application. |
Guido van Rossum | 2d844d1 | 1991-04-07 13:41:50 +0000 | [diff] [blame] | 6 | |
Fred Drake | 5c4012a | 1999-06-25 18:53:23 +0000 | [diff] [blame] | 7 | Each instance is parametrized with two functions, one that is |
| 8 | supposed to return the current time, one that is supposed to |
| 9 | implement a delay. You can implement real-time scheduling by |
| 10 | substituting time and sleep from built-in module time, or you can |
| 11 | implement simulated time by writing your own functions. This can |
| 12 | also be used to integrate scheduling with STDWIN events; the delay |
| 13 | function is allowed to modify the queue. Time can be expressed as |
| 14 | integers or floating point numbers, as long as it is consistent. |
| 15 | |
| 16 | Events are specified by tuples (time, priority, action, argument). |
| 17 | As in UNIX, lower priority numbers mean higher priority; in this |
Raymond Hettinger | bf72b71 | 2004-12-17 13:52:20 +0000 | [diff] [blame] | 18 | way the queue can be maintained as a priority queue. Execution of the |
Fred Drake | 5c4012a | 1999-06-25 18:53:23 +0000 | [diff] [blame] | 19 | event means calling the action function, passing it the argument. |
| 20 | Remember that in Python, multiple function arguments can be packed |
| 21 | in a tuple. The action function may be an instance method so it |
| 22 | has another way to reference private data (besides global variables). |
| 23 | Parameterless functions or methods cannot be used, however. |
| 24 | """ |
Guido van Rossum | 2d844d1 | 1991-04-07 13:41:50 +0000 | [diff] [blame] | 25 | |
Guido van Rossum | 5478cc6 | 1991-11-12 15:37:53 +0000 | [diff] [blame] | 26 | # XXX The timefunc and delayfunc should have been defined as methods |
| 27 | # XXX so you can define new kinds of schedulers using subclassing |
| 28 | # XXX instead of having to define a module or class just to hold |
Fred Drake | 5c4012a | 1999-06-25 18:53:23 +0000 | [diff] [blame] | 29 | # XXX the global state of your particular time and delay functions. |
Guido van Rossum | 5478cc6 | 1991-11-12 15:37:53 +0000 | [diff] [blame] | 30 | |
Raymond Hettinger | bf72b71 | 2004-12-17 13:52:20 +0000 | [diff] [blame] | 31 | import heapq |
Guido van Rossum | 4e16098 | 1992-09-02 20:43:20 +0000 | [diff] [blame] | 32 | |
Skip Montanaro | 0de6580 | 2001-02-15 22:15:14 +0000 | [diff] [blame] | 33 | __all__ = ["scheduler"] |
| 34 | |
Guido van Rossum | ce08448 | 1991-12-26 13:06:29 +0000 | [diff] [blame] | 35 | class scheduler: |
Fred Drake | 5c4012a | 1999-06-25 18:53:23 +0000 | [diff] [blame] | 36 | def __init__(self, timefunc, delayfunc): |
| 37 | """Initialize a new instance, passing the time and delay |
| 38 | functions""" |
| 39 | self.queue = [] |
| 40 | self.timefunc = timefunc |
| 41 | self.delayfunc = delayfunc |
| 42 | |
| 43 | def enterabs(self, time, priority, action, argument): |
| 44 | """Enter a new event in the queue at an absolute time. |
| 45 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 46 | Returns an ID for the event which can be used to remove it, |
| 47 | if necessary. |
Fred Drake | 5c4012a | 1999-06-25 18:53:23 +0000 | [diff] [blame] | 48 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 49 | """ |
Fred Drake | 5c4012a | 1999-06-25 18:53:23 +0000 | [diff] [blame] | 50 | event = time, priority, action, argument |
Raymond Hettinger | bf72b71 | 2004-12-17 13:52:20 +0000 | [diff] [blame] | 51 | heapq.heappush(self.queue, event) |
Fred Drake | 5c4012a | 1999-06-25 18:53:23 +0000 | [diff] [blame] | 52 | return event # The ID |
| 53 | |
| 54 | def enter(self, delay, priority, action, argument): |
| 55 | """A variant that specifies the time as a relative time. |
| 56 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 57 | This is actually the more commonly used interface. |
Fred Drake | 5c4012a | 1999-06-25 18:53:23 +0000 | [diff] [blame] | 58 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 59 | """ |
Fred Drake | 5c4012a | 1999-06-25 18:53:23 +0000 | [diff] [blame] | 60 | time = self.timefunc() + delay |
| 61 | return self.enterabs(time, priority, action, argument) |
| 62 | |
| 63 | def cancel(self, event): |
| 64 | """Remove an event from the queue. |
| 65 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 66 | This must be presented the ID as returned by enter(). |
| 67 | If the event is not in the queue, this raises RuntimeError. |
Fred Drake | 5c4012a | 1999-06-25 18:53:23 +0000 | [diff] [blame] | 68 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 69 | """ |
Fred Drake | 5c4012a | 1999-06-25 18:53:23 +0000 | [diff] [blame] | 70 | self.queue.remove(event) |
Raymond Hettinger | bf72b71 | 2004-12-17 13:52:20 +0000 | [diff] [blame] | 71 | heapq.heapify(self.queue) |
Fred Drake | 5c4012a | 1999-06-25 18:53:23 +0000 | [diff] [blame] | 72 | |
| 73 | def empty(self): |
| 74 | """Check whether the queue is empty.""" |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 75 | return not self.queue |
Fred Drake | 5c4012a | 1999-06-25 18:53:23 +0000 | [diff] [blame] | 76 | |
| 77 | def run(self): |
| 78 | """Execute events until the queue is empty. |
Fred Drake | 5c4012a | 1999-06-25 18:53:23 +0000 | [diff] [blame] | 79 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 80 | When there is a positive delay until the first event, the |
| 81 | delay function is called and the event is left in the queue; |
| 82 | otherwise, the event is removed from the queue and executed |
| 83 | (its action function is called, passing it the argument). If |
| 84 | the delay function returns prematurely, it is simply |
| 85 | restarted. |
Fred Drake | 5c4012a | 1999-06-25 18:53:23 +0000 | [diff] [blame] | 86 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 87 | It is legal for both the delay function and the action |
| 88 | function to to modify the queue or to raise an exception; |
| 89 | exceptions are not caught but the scheduler's state remains |
| 90 | well-defined so run() may be called again. |
Fred Drake | 5c4012a | 1999-06-25 18:53:23 +0000 | [diff] [blame] | 91 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 92 | A questionably hack is added to allow other threads to run: |
| 93 | just after an event is executed, a delay of 0 is executed, to |
| 94 | avoid monopolizing the CPU when other threads are also |
| 95 | runnable. |
| 96 | |
| 97 | """ |
Raymond Hettinger | bf72b71 | 2004-12-17 13:52:20 +0000 | [diff] [blame] | 98 | # localize variable access to minimize overhead |
| 99 | # and to improve thread safety |
Fred Drake | 5c4012a | 1999-06-25 18:53:23 +0000 | [diff] [blame] | 100 | q = self.queue |
Raymond Hettinger | bf72b71 | 2004-12-17 13:52:20 +0000 | [diff] [blame] | 101 | delayfunc = self.delayfunc |
| 102 | timefunc = self.timefunc |
| 103 | pop = heapq.heappop |
Fred Drake | 5c4012a | 1999-06-25 18:53:23 +0000 | [diff] [blame] | 104 | while q: |
Raymond Hettinger | bf72b71 | 2004-12-17 13:52:20 +0000 | [diff] [blame] | 105 | time, priority, action, argument = checked_event = q[0] |
| 106 | now = timefunc() |
Fred Drake | 5c4012a | 1999-06-25 18:53:23 +0000 | [diff] [blame] | 107 | if now < time: |
Raymond Hettinger | bf72b71 | 2004-12-17 13:52:20 +0000 | [diff] [blame] | 108 | delayfunc(time - now) |
Fred Drake | 5c4012a | 1999-06-25 18:53:23 +0000 | [diff] [blame] | 109 | else: |
Raymond Hettinger | bf72b71 | 2004-12-17 13:52:20 +0000 | [diff] [blame] | 110 | event = pop(q) |
| 111 | # Verify that the event was not removed or altered |
| 112 | # by another thread after we last looked at q[0]. |
| 113 | if event is checked_event: |
| 114 | void = action(*argument) |
| 115 | delayfunc(0) # Let other threads run |
| 116 | else: |
| 117 | heapq.heappush(event) |