blob: ba5f33a6ae1e0bacc4ba3cdc0fddc21bcc68c8f3 [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
18way the queue can be maintained fully sorted. Execution of the
19event means calling the action function, passing it the argument.
20Remember that in Python, multiple function arguments can be packed
21in a tuple. The action function may be an instance method so it
22has another way to reference private data (besides global variables).
23Parameterless functions or methods cannot be used, however.
24"""
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
Guido van Rossum4e160981992-09-02 20:43:20 +000031import bisect
32
Skip Montanaro0de65802001-02-15 22:15:14 +000033__all__ = ["scheduler"]
34
Guido van Rossumce084481991-12-26 13:06:29 +000035class scheduler:
Fred Drake5c4012a1999-06-25 18:53:23 +000036 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 Peters495ad3c2001-01-15 01:36:40 +000046 Returns an ID for the event which can be used to remove it,
47 if necessary.
Fred Drake5c4012a1999-06-25 18:53:23 +000048
Tim Peters495ad3c2001-01-15 01:36:40 +000049 """
Fred Drake5c4012a1999-06-25 18:53:23 +000050 event = time, priority, action, argument
51 bisect.insort(self.queue, event)
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 Peters495ad3c2001-01-15 01:36:40 +000057 This is actually the more commonly used interface.
Fred Drake5c4012a1999-06-25 18:53:23 +000058
Tim Peters495ad3c2001-01-15 01:36:40 +000059 """
Fred Drake5c4012a1999-06-25 18:53:23 +000060 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 Peters495ad3c2001-01-15 01:36:40 +000066 This must be presented the ID as returned by enter().
67 If the event is not in the queue, this raises RuntimeError.
Fred Drake5c4012a1999-06-25 18:53:23 +000068
Tim Peters495ad3c2001-01-15 01:36:40 +000069 """
Fred Drake5c4012a1999-06-25 18:53:23 +000070 self.queue.remove(event)
71
72 def empty(self):
73 """Check whether the queue is empty."""
74 return len(self.queue) == 0
75
76 def run(self):
77 """Execute events until the queue is empty.
Fred Drake5c4012a1999-06-25 18:53:23 +000078
Tim Peters495ad3c2001-01-15 01:36:40 +000079 When there is a positive delay until the first event, the
80 delay function is called and the event is left in the queue;
81 otherwise, the event is removed from the queue and executed
82 (its action function is called, passing it the argument). If
83 the delay function returns prematurely, it is simply
84 restarted.
Fred Drake5c4012a1999-06-25 18:53:23 +000085
Tim Peters495ad3c2001-01-15 01:36:40 +000086 It is legal for both the delay function and the action
87 function to to modify the queue or to raise an exception;
88 exceptions are not caught but the scheduler's state remains
89 well-defined so run() may be called again.
Fred Drake5c4012a1999-06-25 18:53:23 +000090
Tim Peters495ad3c2001-01-15 01:36:40 +000091 A questionably hack is added to allow other threads to run:
92 just after an event is executed, a delay of 0 is executed, to
93 avoid monopolizing the CPU when other threads are also
94 runnable.
95
96 """
Fred Drake5c4012a1999-06-25 18:53:23 +000097 q = self.queue
98 while q:
99 time, priority, action, argument = q[0]
100 now = self.timefunc()
101 if now < time:
102 self.delayfunc(time - now)
103 else:
104 del q[0]
105 void = apply(action, argument)
106 self.delayfunc(0) # Let other threads run