Issue #1861: Add read-only attribute listing upcoming events in the order they will be run.

This commit is contained in:
Raymond Hettinger 2008-01-17 19:31:38 +00:00
parent a35a8b11c3
commit 44bd6c0a4f
3 changed files with 33 additions and 8 deletions

View file

@ -47,7 +47,7 @@ Example::
Scheduler Objects Scheduler Objects
----------------- -----------------
:class:`scheduler` instances have the following methods: :class:`scheduler` instances have the following methods and attributes:
.. method:: scheduler.enterabs(time, priority, action, argument) .. method:: scheduler.enterabs(time, priority, action, argument)
@ -98,3 +98,8 @@ Scheduler Objects
the calling code is responsible for canceling events which are no longer the calling code is responsible for canceling events which are no longer
pertinent. pertinent.
.. attribute:: scheduler.queue
Read-only attribute returning a list of upcoming events in the order they
will be run. Each event is shown as a :term:`named tuple` with the
following fields: time, priority, action, argument.

View file

@ -29,14 +29,17 @@ has another way to reference private data (besides global variables).
# XXX the global state of your particular time and delay functions. # XXX the global state of your particular time and delay functions.
import heapq import heapq
from collections import namedtuple
__all__ = ["scheduler"] __all__ = ["scheduler"]
Event = namedtuple('Event', 'time, priority, action, argument')
class scheduler: class scheduler:
def __init__(self, timefunc, delayfunc): def __init__(self, timefunc, delayfunc):
"""Initialize a new instance, passing the time and delay """Initialize a new instance, passing the time and delay
functions""" functions"""
self.queue = [] self._queue = []
self.timefunc = timefunc self.timefunc = timefunc
self.delayfunc = delayfunc self.delayfunc = delayfunc
@ -47,8 +50,8 @@ class scheduler:
if necessary. if necessary.
""" """
event = time, priority, action, argument event = Event(time, priority, action, argument)
heapq.heappush(self.queue, event) heapq.heappush(self._queue, event)
return event # The ID return event # The ID
def enter(self, delay, priority, action, argument): def enter(self, delay, priority, action, argument):
@ -67,12 +70,12 @@ class scheduler:
If the event is not in the queue, this raises RuntimeError. If the event is not in the queue, this raises RuntimeError.
""" """
self.queue.remove(event) self._queue.remove(event)
heapq.heapify(self.queue) heapq.heapify(self._queue)
def empty(self): def empty(self):
"""Check whether the queue is empty.""" """Check whether the queue is empty."""
return not self.queue return not self._queue
def run(self): def run(self):
"""Execute events until the queue is empty. """Execute events until the queue is empty.
@ -97,7 +100,7 @@ class scheduler:
""" """
# localize variable access to minimize overhead # localize variable access to minimize overhead
# and to improve thread safety # and to improve thread safety
q = self.queue q = self._queue
delayfunc = self.delayfunc delayfunc = self.delayfunc
timefunc = self.timefunc timefunc = self.timefunc
pop = heapq.heappop pop = heapq.heappop
@ -115,3 +118,17 @@ class scheduler:
delayfunc(0) # Let other threads run delayfunc(0) # Let other threads run
else: else:
heapq.heappush(event) heapq.heappush(event)
@property
def queue(self):
"""An ordered list of upcoming events.
Events are named tuples with fields for:
time, priority, action, arguments
"""
# Use heapq to sort the queue rather than using 'sorted(self._queue)'.
# With heapq, two events scheduled at the same time will show in
# the actual order they would be retrieved.
events = self._queue[:]
return map(heapq.heappop, [events]*len(events))

View file

@ -364,6 +364,9 @@ Core and builtins
Library Library
------- -------
- #1861: Added an attribute to the sched module which returns an ordered
list of upcoming events (displayed as named tuples).
- #1837: The queue module now also supports a LIFO queue and a priority queue. - #1837: The queue module now also supports a LIFO queue and a priority queue.
- Issue #1831: ctypes now raises a TypeError if conflicting positional - Issue #1831: ctypes now raises a TypeError if conflicting positional