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 | |
Serhiy Storchaka | e912496 | 2012-12-29 20:57:52 +0200 | [diff] [blame] | 16 | Events are specified by tuples (time, priority, action, argument, kwargs). |
Fred Drake | 5c4012a | 1999-06-25 18:53:23 +0000 | [diff] [blame] | 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 |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 19 | event means calling the action function, passing it the argument |
| 20 | sequence in "argument" (remember that in Python, multiple function |
Serhiy Storchaka | e912496 | 2012-12-29 20:57:52 +0200 | [diff] [blame] | 21 | arguments are be packed in a sequence) and keyword parameters in "kwargs". |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 22 | The action function may be an instance method so it |
Fred Drake | 5c4012a | 1999-06-25 18:53:23 +0000 | [diff] [blame] | 23 | has another way to reference private data (besides global variables). |
Fred Drake | 5c4012a | 1999-06-25 18:53:23 +0000 | [diff] [blame] | 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 | |
Giampaolo Rodola' | be55d99 | 2011-11-22 13:33:34 +0100 | [diff] [blame] | 31 | import time |
Raymond Hettinger | bf72b71 | 2004-12-17 13:52:20 +0000 | [diff] [blame] | 32 | import heapq |
Christian Heimes | 679db4a | 2008-01-18 09:56:22 +0000 | [diff] [blame] | 33 | from collections import namedtuple |
Giampaolo Rodola' | 2fa2281 | 2011-12-19 19:12:01 +0100 | [diff] [blame] | 34 | try: |
| 35 | import threading |
Brett Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame] | 36 | except ImportError: |
Giampaolo Rodola' | 2fa2281 | 2011-12-19 19:12:01 +0100 | [diff] [blame] | 37 | import dummy_threading as threading |
Victor Stinner | ae58649 | 2014-09-02 23:18:25 +0200 | [diff] [blame] | 38 | from time import monotonic as _time |
Guido van Rossum | 4e16098 | 1992-09-02 20:43:20 +0000 | [diff] [blame] | 39 | |
Skip Montanaro | 0de6580 | 2001-02-15 22:15:14 +0000 | [diff] [blame] | 40 | __all__ = ["scheduler"] |
| 41 | |
Giampaolo Rodola' | be55d99 | 2011-11-22 13:33:34 +0100 | [diff] [blame] | 42 | class Event(namedtuple('Event', 'time, priority, action, argument, kwargs')): |
Raymond Hettinger | 8f40e09 | 2009-04-24 18:43:43 +0000 | [diff] [blame] | 43 | def __eq__(s, o): return (s.time, s.priority) == (o.time, o.priority) |
Raymond Hettinger | 8f40e09 | 2009-04-24 18:43:43 +0000 | [diff] [blame] | 44 | def __lt__(s, o): return (s.time, s.priority) < (o.time, o.priority) |
| 45 | def __le__(s, o): return (s.time, s.priority) <= (o.time, o.priority) |
| 46 | def __gt__(s, o): return (s.time, s.priority) > (o.time, o.priority) |
| 47 | def __ge__(s, o): return (s.time, s.priority) >= (o.time, o.priority) |
Christian Heimes | 679db4a | 2008-01-18 09:56:22 +0000 | [diff] [blame] | 48 | |
Raymond Hettinger | 5b798ab | 2015-08-17 22:04:45 -0700 | [diff] [blame] | 49 | Event.time.__doc__ = ('''Numeric type compatible with the return value of the |
| 50 | timefunc function passed to the constructor.''') |
| 51 | Event.priority.__doc__ = ('''Events scheduled for the same time will be executed |
| 52 | in the order of their priority.''') |
| 53 | Event.action.__doc__ = ('''Executing the event means executing |
| 54 | action(*argument, **kwargs)''') |
| 55 | Event.argument.__doc__ = ('''argument is a sequence holding the positional |
| 56 | arguments for the action.''') |
| 57 | Event.kwargs.__doc__ = ('''kwargs is a dictionary holding the keyword |
| 58 | arguments for the action.''') |
| 59 | |
Serhiy Storchaka | c04957b | 2012-12-29 21:13:45 +0200 | [diff] [blame] | 60 | _sentinel = object() |
| 61 | |
Guido van Rossum | ce08448 | 1991-12-26 13:06:29 +0000 | [diff] [blame] | 62 | class scheduler: |
Giampaolo Rodola' | be55d99 | 2011-11-22 13:33:34 +0100 | [diff] [blame] | 63 | |
Victor Stinner | 949d8c9 | 2012-05-30 13:30:32 +0200 | [diff] [blame] | 64 | def __init__(self, timefunc=_time, delayfunc=time.sleep): |
Fred Drake | 5c4012a | 1999-06-25 18:53:23 +0000 | [diff] [blame] | 65 | """Initialize a new instance, passing the time and delay |
| 66 | functions""" |
Christian Heimes | 679db4a | 2008-01-18 09:56:22 +0000 | [diff] [blame] | 67 | self._queue = [] |
Giampaolo Rodola' | 73520d5 | 2011-12-14 13:34:26 +0100 | [diff] [blame] | 68 | self._lock = threading.RLock() |
Fred Drake | 5c4012a | 1999-06-25 18:53:23 +0000 | [diff] [blame] | 69 | self.timefunc = timefunc |
| 70 | self.delayfunc = delayfunc |
| 71 | |
Serhiy Storchaka | c04957b | 2012-12-29 21:13:45 +0200 | [diff] [blame] | 72 | def enterabs(self, time, priority, action, argument=(), kwargs=_sentinel): |
Fred Drake | 5c4012a | 1999-06-25 18:53:23 +0000 | [diff] [blame] | 73 | """Enter a new event in the queue at an absolute time. |
| 74 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 75 | Returns an ID for the event which can be used to remove it, |
| 76 | if necessary. |
Fred Drake | 5c4012a | 1999-06-25 18:53:23 +0000 | [diff] [blame] | 77 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 78 | """ |
Serhiy Storchaka | c04957b | 2012-12-29 21:13:45 +0200 | [diff] [blame] | 79 | if kwargs is _sentinel: |
| 80 | kwargs = {} |
Serhiy Storchaka | d07db96 | 2012-12-29 21:46:37 +0200 | [diff] [blame] | 81 | event = Event(time, priority, action, argument, kwargs) |
Giampaolo Rodola' | 73520d5 | 2011-12-14 13:34:26 +0100 | [diff] [blame] | 82 | with self._lock: |
Giampaolo Rodola' | 73520d5 | 2011-12-14 13:34:26 +0100 | [diff] [blame] | 83 | heapq.heappush(self._queue, event) |
Serhiy Storchaka | d07db96 | 2012-12-29 21:46:37 +0200 | [diff] [blame] | 84 | return event # The ID |
Fred Drake | 5c4012a | 1999-06-25 18:53:23 +0000 | [diff] [blame] | 85 | |
Serhiy Storchaka | c04957b | 2012-12-29 21:13:45 +0200 | [diff] [blame] | 86 | def enter(self, delay, priority, action, argument=(), kwargs=_sentinel): |
Fred Drake | 5c4012a | 1999-06-25 18:53:23 +0000 | [diff] [blame] | 87 | """A variant that specifies the time as a relative time. |
| 88 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 89 | This is actually the more commonly used interface. |
Fred Drake | 5c4012a | 1999-06-25 18:53:23 +0000 | [diff] [blame] | 90 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 91 | """ |
Serhiy Storchaka | d07db96 | 2012-12-29 21:46:37 +0200 | [diff] [blame] | 92 | time = self.timefunc() + delay |
| 93 | return self.enterabs(time, priority, action, argument, kwargs) |
Fred Drake | 5c4012a | 1999-06-25 18:53:23 +0000 | [diff] [blame] | 94 | |
| 95 | def cancel(self, event): |
| 96 | """Remove an event from the queue. |
| 97 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 98 | This must be presented the ID as returned by enter(). |
Georg Brandl | c38a000 | 2009-05-26 07:51:03 +0000 | [diff] [blame] | 99 | If the event is not in the queue, this raises ValueError. |
Fred Drake | 5c4012a | 1999-06-25 18:53:23 +0000 | [diff] [blame] | 100 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 101 | """ |
Giampaolo Rodola' | 73520d5 | 2011-12-14 13:34:26 +0100 | [diff] [blame] | 102 | with self._lock: |
| 103 | self._queue.remove(event) |
| 104 | heapq.heapify(self._queue) |
Fred Drake | 5c4012a | 1999-06-25 18:53:23 +0000 | [diff] [blame] | 105 | |
| 106 | def empty(self): |
| 107 | """Check whether the queue is empty.""" |
Giampaolo Rodola' | 73520d5 | 2011-12-14 13:34:26 +0100 | [diff] [blame] | 108 | with self._lock: |
| 109 | return not self._queue |
Fred Drake | 5c4012a | 1999-06-25 18:53:23 +0000 | [diff] [blame] | 110 | |
Giampaolo Rodola' | 556ba04 | 2011-12-14 14:38:45 +0100 | [diff] [blame] | 111 | def run(self, blocking=True): |
Fred Drake | 5c4012a | 1999-06-25 18:53:23 +0000 | [diff] [blame] | 112 | """Execute events until the queue is empty. |
Giampaolo Rodola' | 556ba04 | 2011-12-14 14:38:45 +0100 | [diff] [blame] | 113 | If blocking is False executes the scheduled events due to |
Giampaolo Rodola' | a4e0188 | 2012-03-15 13:05:41 +0100 | [diff] [blame] | 114 | expire soonest (if any) and then return the deadline of the |
| 115 | next scheduled call in the scheduler. |
Fred Drake | 5c4012a | 1999-06-25 18:53:23 +0000 | [diff] [blame] | 116 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 117 | When there is a positive delay until the first event, the |
| 118 | delay function is called and the event is left in the queue; |
| 119 | otherwise, the event is removed from the queue and executed |
| 120 | (its action function is called, passing it the argument). If |
| 121 | the delay function returns prematurely, it is simply |
| 122 | restarted. |
Fred Drake | 5c4012a | 1999-06-25 18:53:23 +0000 | [diff] [blame] | 123 | |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 124 | It is legal for both the delay function and the action |
Ezio Melotti | e130a52 | 2011-10-19 10:58:56 +0300 | [diff] [blame] | 125 | function to modify the queue or to raise an exception; |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 126 | exceptions are not caught but the scheduler's state remains |
| 127 | well-defined so run() may be called again. |
Fred Drake | 5c4012a | 1999-06-25 18:53:23 +0000 | [diff] [blame] | 128 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 129 | A questionable hack is added to allow other threads to run: |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 130 | just after an event is executed, a delay of 0 is executed, to |
| 131 | avoid monopolizing the CPU when other threads are also |
| 132 | runnable. |
| 133 | |
| 134 | """ |
Raymond Hettinger | bf72b71 | 2004-12-17 13:52:20 +0000 | [diff] [blame] | 135 | # localize variable access to minimize overhead |
| 136 | # and to improve thread safety |
Serhiy Storchaka | f2b9cf4 | 2012-12-29 21:34:11 +0200 | [diff] [blame] | 137 | lock = self._lock |
| 138 | q = self._queue |
| 139 | delayfunc = self.delayfunc |
| 140 | timefunc = self.timefunc |
| 141 | pop = heapq.heappop |
| 142 | while True: |
| 143 | with lock: |
| 144 | if not q: |
| 145 | break |
| 146 | time, priority, action, argument, kwargs = q[0] |
Giampaolo Rodola' | 73520d5 | 2011-12-14 13:34:26 +0100 | [diff] [blame] | 147 | now = timefunc() |
Serhiy Storchaka | f2b9cf4 | 2012-12-29 21:34:11 +0200 | [diff] [blame] | 148 | if time > now: |
| 149 | delay = True |
Raymond Hettinger | bf72b71 | 2004-12-17 13:52:20 +0000 | [diff] [blame] | 150 | else: |
Serhiy Storchaka | f2b9cf4 | 2012-12-29 21:34:11 +0200 | [diff] [blame] | 151 | delay = False |
| 152 | pop(q) |
| 153 | if delay: |
| 154 | if not blocking: |
| 155 | return time - now |
| 156 | delayfunc(time - now) |
| 157 | else: |
| 158 | action(*argument, **kwargs) |
| 159 | delayfunc(0) # Let other threads run |
Christian Heimes | 679db4a | 2008-01-18 09:56:22 +0000 | [diff] [blame] | 160 | |
| 161 | @property |
| 162 | def queue(self): |
| 163 | """An ordered list of upcoming events. |
| 164 | |
| 165 | Events are named tuples with fields for: |
Serhiy Storchaka | e912496 | 2012-12-29 20:57:52 +0200 | [diff] [blame] | 166 | time, priority, action, arguments, kwargs |
Christian Heimes | 679db4a | 2008-01-18 09:56:22 +0000 | [diff] [blame] | 167 | |
| 168 | """ |
| 169 | # Use heapq to sort the queue rather than using 'sorted(self._queue)'. |
| 170 | # With heapq, two events scheduled at the same time will show in |
| 171 | # the actual order they would be retrieved. |
Giampaolo Rodola' | 73520d5 | 2011-12-14 13:34:26 +0100 | [diff] [blame] | 172 | with self._lock: |
| 173 | events = self._queue[:] |
Raymond Hettinger | 468bcaf | 2013-07-13 22:48:49 -0700 | [diff] [blame] | 174 | return list(map(heapq.heappop, [events]*len(events))) |