Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(20)

Delta Between Two Patch Sets: Lib/sched.py

Issue 22043: Use a monotonic clock to compute timeouts
Left Patch Set: Created 3 years, 3 months ago
Right Patch Set: Created 3 years, 2 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/queue.py ('k') | Lib/socketserver.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
(no file at all)
1 """A generally useful event scheduler class. 1 """A generally useful event scheduler class.
2 2
3 Each instance of this class manages its own queue. 3 Each instance of this class manages its own queue.
4 No multi-threading is implied; you are supposed to hack that 4 No multi-threading is implied; you are supposed to hack that
5 yourself, or use a single instance per application. 5 yourself, or use a single instance per application.
6 6
7 Each instance is parametrized with two functions, one that is 7 Each instance is parametrized with two functions, one that is
8 supposed to return the current time, one that is supposed to 8 supposed to return the current time, one that is supposed to
9 implement a delay. You can implement real-time scheduling by 9 implement a delay. You can implement real-time scheduling by
10 substituting time and sleep from built-in module time, or you can 10 substituting time and sleep from built-in module time, or you can
(...skipping 17 matching lines...) Expand all
28 # XXX instead of having to define a module or class just to hold 28 # XXX instead of having to define a module or class just to hold
29 # XXX the global state of your particular time and delay functions. 29 # XXX the global state of your particular time and delay functions.
30 30
31 import time 31 import time
32 import heapq 32 import heapq
33 from collections import namedtuple 33 from collections import namedtuple
34 try: 34 try:
35 import threading 35 import threading
36 except ImportError: 36 except ImportError:
37 import dummy_threading as threading 37 import dummy_threading as threading
38 try: 38 from time import monotonic as _time
39 from time import monotonic as _time
40 except ImportError:
41 from time import time as _time
42 39
43 __all__ = ["scheduler"] 40 __all__ = ["scheduler"]
44 41
45 class Event(namedtuple('Event', 'time, priority, action, argument, kwargs')): 42 class Event(namedtuple('Event', 'time, priority, action, argument, kwargs')):
46 def __eq__(s, o): return (s.time, s.priority) == (o.time, o.priority) 43 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) 44 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) 45 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) 46 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) 47 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) 48 def __ge__(s, o): return (s.time, s.priority) >= (o.time, o.priority)
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 Events are named tuples with fields for: 155 Events are named tuples with fields for:
159 time, priority, action, arguments, kwargs 156 time, priority, action, arguments, kwargs
160 157
161 """ 158 """
162 # Use heapq to sort the queue rather than using 'sorted(self._queue)'. 159 # Use heapq to sort the queue rather than using 'sorted(self._queue)'.
163 # With heapq, two events scheduled at the same time will show in 160 # With heapq, two events scheduled at the same time will show in
164 # the actual order they would be retrieved. 161 # the actual order they would be retrieved.
165 with self._lock: 162 with self._lock:
166 events = self._queue[:] 163 events = self._queue[:]
167 return list(map(heapq.heappop, [events]*len(events))) 164 return list(map(heapq.heappop, [events]*len(events)))
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7