annotate Lib/sched.py @ 5817:48b94de85bf3 legacy-trunk

Extensive changes by AMK.
author Guido van Rossum <guido@python.org>
date Thu, 17 Jul 1997 16:29:42 +0000
parents 1940892d2fe3
children 7ed6fba8265c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
195
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # Module sched -- a generally useful event scheduler class
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 # Each instance of this class manages its own queue.
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 # No multi-threading is implied; you are supposed to hack that
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 # yourself, or use a single instance per application.
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 #
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 # Each instance is parametrized with two functions, one that is
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 # supposed to return the current time, one that is supposed to
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
9 # implement a delay. You can implement real-time scheduling by
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
10 # substituting time and sleep from built-in module time, or you can
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
11 # implement simulated time by writing your own functions. This can
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
12 # also be used to integrate scheduling with STDWIN events; the delay
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
13 # function is allowed to modify the queue. Time can be expressed as
218
3429c945d817 Delay zero after successful action.
Guido van Rossum <guido@python.org>
parents: 195
diff changeset
14 # integers or floating point numbers, as long as it is consistent.
195
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
15
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 # Events are specified by tuples (time, priority, action, argument).
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 # As in UNIX, lower priority numbers mean higher priority; in this
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 # way the queue can be maintained fully sorted. Execution of the
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 # event means calling the action function, passing it the argument.
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 # Remember that in Python, multiple function arguments can be packed
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 # in a tuple. The action function may be an instance method so it
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 # has another way to reference private data (besides global variables).
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 # Parameterless functions or methods cannot be used, however.
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
24
429
bb308e7b0039 Added a comment about a possible interface change.
Guido van Rossum <guido@python.org>
parents: 218
diff changeset
25 # XXX The timefunc and delayfunc should have been defined as methods
bb308e7b0039 Added a comment about a possible interface change.
Guido van Rossum <guido@python.org>
parents: 218
diff changeset
26 # XXX so you can define new kinds of schedulers using subclassing
bb308e7b0039 Added a comment about a possible interface change.
Guido van Rossum <guido@python.org>
parents: 218
diff changeset
27 # XXX instead of having to define a module or class just to hold
bb308e7b0039 Added a comment about a possible interface change.
Guido van Rossum <guido@python.org>
parents: 218
diff changeset
28 # XXX the global state of your particular time and delay functtions.
bb308e7b0039 Added a comment about a possible interface change.
Guido van Rossum <guido@python.org>
parents: 218
diff changeset
29
1006
903071377d29 Added a _v21 def to FL.py and added two new input field types
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
30 import bisect
903071377d29 Added a _v21 def to FL.py and added two new input field types
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
31
531
8331076cfe15 New class syntax.
Guido van Rossum <guido@python.org>
parents: 429
diff changeset
32 class scheduler:
195
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 #
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 # Initialize a new instance, passing the time and delay functions
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 #
1435
560ec7030817 * Mass change: get rid of all init() methods, in favor of __init__()
Guido van Rossum <guido@python.org>
parents: 1110
diff changeset
36 def __init__(self, timefunc, delayfunc):
195
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 self.queue = []
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 self.timefunc = timefunc
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 self.delayfunc = delayfunc
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 #
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 # Enter a new event in the queue at an absolute time.
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 # Returns an ID for the event which can be used
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 # to remove it, if necessary.
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 #
1110
1d06b1499eb9 * Got entirely rid of path.py.
Guido van Rossum <guido@python.org>
parents: 1006
diff changeset
45 def enterabs(self, time, priority, action, argument):
1d06b1499eb9 * Got entirely rid of path.py.
Guido van Rossum <guido@python.org>
parents: 1006
diff changeset
46 event = time, priority, action, argument
1006
903071377d29 Added a _v21 def to FL.py and added two new input field types
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
47 bisect.insort(self.queue, event)
195
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 return event # The ID
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 #
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 # A variant that specifies the time as a relative time.
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 # This is actually the more commonly used interface.
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 #
1110
1d06b1499eb9 * Got entirely rid of path.py.
Guido van Rossum <guido@python.org>
parents: 1006
diff changeset
53 def enter(self, delay, priority, action, argument):
195
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 time = self.timefunc() + delay
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 return self.enterabs(time, priority, action, argument)
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 #
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 # Remove an event from the queue.
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 # This must be presented the ID as returned by enter().
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 # If the event is not in the queue, this raises RuntimeError.
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 #
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 def cancel(self, event):
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 self.queue.remove(event)
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 #
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 # Check whether the queue is empty.
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 #
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 def empty(self):
559
683edb0c4463 New == syntax
Guido van Rossum <guido@python.org>
parents: 531
diff changeset
67 return len(self.queue) == 0
195
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 #
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 # Run: execute events until the queue is empty.
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 #
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 # When there is a positive delay until the first event, the
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 # delay function is called and the event is left in the queue;
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 # otherwise, the event is removed from the queue and executed
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 # (its action function is called, passing it the argument).
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 # If the delay function returns prematurely, it is simply
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 # restarted.
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 #
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 # It is legal for both the delay function and the action
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 # function to to modify the queue or to raise an exception;
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 # exceptions are not caught but the scheduler's state
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 # remains well-defined so run() may be called again.
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 #
218
3429c945d817 Delay zero after successful action.
Guido van Rossum <guido@python.org>
parents: 195
diff changeset
83 # A questionably hack is added to allow other threads to run:
3429c945d817 Delay zero after successful action.
Guido van Rossum <guido@python.org>
parents: 195
diff changeset
84 # just after an event is executed, a delay of 0 is executed,
3429c945d817 Delay zero after successful action.
Guido van Rossum <guido@python.org>
parents: 195
diff changeset
85 # to avoid monopolizing the CPU when other threads are also
3429c945d817 Delay zero after successful action.
Guido van Rossum <guido@python.org>
parents: 195
diff changeset
86 # runnable.
3429c945d817 Delay zero after successful action.
Guido van Rossum <guido@python.org>
parents: 195
diff changeset
87 #
195
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 def run(self):
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 q = self.queue
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 while q:
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 time, priority, action, argument = q[0]
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 now = self.timefunc()
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 if now < time:
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 self.delayfunc(time - now)
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 else:
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 del q[0]
1110
1d06b1499eb9 * Got entirely rid of path.py.
Guido van Rossum <guido@python.org>
parents: 1006
diff changeset
97 void = apply(action, argument)
218
3429c945d817 Delay zero after successful action.
Guido van Rossum <guido@python.org>
parents: 195
diff changeset
98 self.delayfunc(0) # Let other threads run
195
45f43f755e5c Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 #