blob: 5551f71df6c0241cb8c5e5754136ce8c0b1c749c [file] [log] [blame]
Guido van Rossum4b8c6ea2000-02-04 15:39:30 +00001"""A generally useful event scheduler class.
Guido van Rossum2d844d11991-04-07 13:41:50 +00002
Fred Drake5c4012a1999-06-25 18:53:23 +00003Each instance of this class manages its own queue.
4No multi-threading is implied; you are supposed to hack that
5yourself, or use a single instance per application.
Guido van Rossum2d844d11991-04-07 13:41:50 +00006
Fred Drake5c4012a1999-06-25 18:53:23 +00007Each instance is parametrized with two functions, one that is
8supposed to return the current time, one that is supposed to
9implement a delay. You can implement real-time scheduling by
10substituting time and sleep from built-in module time, or you can
11implement simulated time by writing your own functions. This can
12also be used to integrate scheduling with STDWIN events; the delay
13function is allowed to modify the queue. Time can be expressed as
14integers or floating point numbers, as long as it is consistent.
15
16Events are specified by tuples (time, priority, action, argument).
17As in UNIX, lower priority numbers mean higher priority; in this
Raymond Hettingerbf72b712004-12-17 13:52:20 +000018way the queue can be maintained as a priority queue. Execution of the
Guido van Rossum8ce8a782007-11-01 19:42:39 +000019event means calling the action function, passing it the argument
20sequence in "argument" (remember that in Python, multiple function
21arguments are be packed in a sequence).
22The action function may be an instance method so it
Fred Drake5c4012a1999-06-25 18:53:23 +000023has another way to reference private data (besides global variables).
Fred Drake5c4012a1999-06-25 18:53:23 +000024"""
Guido van Rossum2d844d11991-04-07 13:41:50 +000025
Guido van Rossum5478cc61991-11-12 15:37:53 +000026# 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 Drake5c4012a1999-06-25 18:53:23 +000029# XXX the global state of your particular time and delay functions.
Guido van Rossum5478cc61991-11-12 15:37:53 +000030
Giampaolo Rodola'be55d992011-11-22 13:33:34 +010031import time
Raymond Hettingerbf72b712004-12-17 13:52:20 +000032import heapq
Christian Heimes679db4a2008-01-18 09:56:22 +000033from collections import namedtuple
Giampaolo Rodola'2fa22812011-12-19 19:12:01 +010034try:
35 import threading
36except ImportError:
37 import dummy_threading as threading
Victor Stinner949d8c92012-05-30 13:30:32 +020038try:
39 from time import monotonic as _time
40except ImportError:
41 from time import time as _time
Guido van Rossum4e160981992-09-02 20:43:20 +000042
Skip Montanaro0de65802001-02-15 22:15:14 +000043__all__ = ["scheduler"]
44
Giampaolo Rodola'be55d992011-11-22 13:33:34 +010045class Event(namedtuple('Event', 'time, priority, action, argument, kwargs')):
Raymond Hettinger8f40e092009-04-24 18:43:43 +000046 def __eq__(s, o): return (s.time, s.priority) == (o.time, o.priority)
47 def __ne__(s, o): return (s.time, s.priority) != (o.time, o.priority)
48 def __lt__(s, o): return (s.time, s.priority) < (o.time, o.priority)
49 def __le__(s, o): return (s.time, s.priority) <= (o.time, o.priority)
50 def __gt__(s, o): return (s.time, s.priority) > (o.time, o.priority)
51 def __ge__(s, o): return (s.time, s.priority) >= (o.time, o.priority)
Christian Heimes679db4a2008-01-18 09:56:22 +000052
Guido van Rossumce084481991-12-26 13:06:29 +000053class scheduler:
Giampaolo Rodola'be55d992011-11-22 13:33:34 +010054
Victor Stinner949d8c92012-05-30 13:30:32 +020055 def __init__(self, timefunc=_time, delayfunc=time.sleep):
Fred Drake5c4012a1999-06-25 18:53:23 +000056 """Initialize a new instance, passing the time and delay
57 functions"""
Christian Heimes679db4a2008-01-18 09:56:22 +000058 self._queue = []
Giampaolo Rodola'73520d52011-12-14 13:34:26 +010059 self._lock = threading.RLock()
Fred Drake5c4012a1999-06-25 18:53:23 +000060 self.timefunc = timefunc
61 self.delayfunc = delayfunc
62
Giampaolo Rodola'be55d992011-11-22 13:33:34 +010063 def enterabs(self, time, priority, action, argument=[], kwargs={}):
Fred Drake5c4012a1999-06-25 18:53:23 +000064 """Enter a new event in the queue at an absolute time.
65
Tim Peters495ad3c2001-01-15 01:36:40 +000066 Returns an ID for the event which can be used to remove it,
67 if necessary.
Fred Drake5c4012a1999-06-25 18:53:23 +000068
Tim Peters495ad3c2001-01-15 01:36:40 +000069 """
Giampaolo Rodola'73520d52011-12-14 13:34:26 +010070 with self._lock:
71 event = Event(time, priority, action, argument, kwargs)
72 heapq.heappush(self._queue, event)
73 return event # The ID
Fred Drake5c4012a1999-06-25 18:53:23 +000074
Giampaolo Rodola'be55d992011-11-22 13:33:34 +010075 def enter(self, delay, priority, action, argument=[], kwargs={}):
Fred Drake5c4012a1999-06-25 18:53:23 +000076 """A variant that specifies the time as a relative time.
77
Tim Peters495ad3c2001-01-15 01:36:40 +000078 This is actually the more commonly used interface.
Fred Drake5c4012a1999-06-25 18:53:23 +000079
Tim Peters495ad3c2001-01-15 01:36:40 +000080 """
Giampaolo Rodola'73520d52011-12-14 13:34:26 +010081 with self._lock:
82 time = self.timefunc() + delay
83 return self.enterabs(time, priority, action, argument, kwargs)
Fred Drake5c4012a1999-06-25 18:53:23 +000084
85 def cancel(self, event):
86 """Remove an event from the queue.
87
Tim Peters495ad3c2001-01-15 01:36:40 +000088 This must be presented the ID as returned by enter().
Georg Brandlc38a0002009-05-26 07:51:03 +000089 If the event is not in the queue, this raises ValueError.
Fred Drake5c4012a1999-06-25 18:53:23 +000090
Tim Peters495ad3c2001-01-15 01:36:40 +000091 """
Giampaolo Rodola'73520d52011-12-14 13:34:26 +010092 with self._lock:
93 self._queue.remove(event)
94 heapq.heapify(self._queue)
Fred Drake5c4012a1999-06-25 18:53:23 +000095
96 def empty(self):
97 """Check whether the queue is empty."""
Giampaolo Rodola'73520d52011-12-14 13:34:26 +010098 with self._lock:
99 return not self._queue
Fred Drake5c4012a1999-06-25 18:53:23 +0000100
Giampaolo Rodola'556ba042011-12-14 14:38:45 +0100101 def run(self, blocking=True):
Fred Drake5c4012a1999-06-25 18:53:23 +0000102 """Execute events until the queue is empty.
Giampaolo Rodola'556ba042011-12-14 14:38:45 +0100103 If blocking is False executes the scheduled events due to
Giampaolo Rodola'a4e01882012-03-15 13:05:41 +0100104 expire soonest (if any) and then return the deadline of the
105 next scheduled call in the scheduler.
Fred Drake5c4012a1999-06-25 18:53:23 +0000106
Tim Peters495ad3c2001-01-15 01:36:40 +0000107 When there is a positive delay until the first event, the
108 delay function is called and the event is left in the queue;
109 otherwise, the event is removed from the queue and executed
110 (its action function is called, passing it the argument). If
111 the delay function returns prematurely, it is simply
112 restarted.
Fred Drake5c4012a1999-06-25 18:53:23 +0000113
Tim Peters495ad3c2001-01-15 01:36:40 +0000114 It is legal for both the delay function and the action
Ezio Melottie130a522011-10-19 10:58:56 +0300115 function to modify the queue or to raise an exception;
Tim Peters495ad3c2001-01-15 01:36:40 +0000116 exceptions are not caught but the scheduler's state remains
117 well-defined so run() may be called again.
Fred Drake5c4012a1999-06-25 18:53:23 +0000118
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000119 A questionable hack is added to allow other threads to run:
Tim Peters495ad3c2001-01-15 01:36:40 +0000120 just after an event is executed, a delay of 0 is executed, to
121 avoid monopolizing the CPU when other threads are also
122 runnable.
123
124 """
Raymond Hettingerbf72b712004-12-17 13:52:20 +0000125 # localize variable access to minimize overhead
126 # and to improve thread safety
Giampaolo Rodola'73520d52011-12-14 13:34:26 +0100127 with self._lock:
128 q = self._queue
129 delayfunc = self.delayfunc
130 timefunc = self.timefunc
131 pop = heapq.heappop
132 while q:
133 time, priority, action, argument, kwargs = checked_event = q[0]
134 now = timefunc()
135 if now < time:
Giampaolo Rodola'556ba042011-12-14 14:38:45 +0100136 if not blocking:
Giampaolo Rodola'a4e01882012-03-15 13:05:41 +0100137 return time - now
Giampaolo Rodola'73520d52011-12-14 13:34:26 +0100138 delayfunc(time - now)
Raymond Hettingerbf72b712004-12-17 13:52:20 +0000139 else:
Giampaolo Rodola'73520d52011-12-14 13:34:26 +0100140 event = pop(q)
141 # Verify that the event was not removed or altered
142 # by another thread after we last looked at q[0].
143 if event is checked_event:
144 action(*argument, **kwargs)
145 delayfunc(0) # Let other threads run
146 else:
147 heapq.heappush(q, event)
Christian Heimes679db4a2008-01-18 09:56:22 +0000148
149 @property
150 def queue(self):
151 """An ordered list of upcoming events.
152
153 Events are named tuples with fields for:
154 time, priority, action, arguments
155
156 """
157 # Use heapq to sort the queue rather than using 'sorted(self._queue)'.
158 # With heapq, two events scheduled at the same time will show in
159 # the actual order they would be retrieved.
Giampaolo Rodola'73520d52011-12-14 13:34:26 +0100160 with self._lock:
161 events = self._queue[:]
162 return map(heapq.heappop, [events]*len(events))