Index: Doc/library/queue.rst =================================================================== --- Doc/library/queue.rst (revision 59990) +++ Doc/library/queue.rst (working copy) @@ -6,24 +6,47 @@ :synopsis: A synchronized queue class. -The :mod:`Queue` module implements a multi-producer, multi-consumer FIFO queue. +The :mod:`Queue` module implements multi-producer, multi-consumer queues. It is especially useful in threaded programming when information must be exchanged safely between multiple threads. The :class:`Queue` class in this module implements all the required locking semantics. It depends on the availability of thread support in Python; see the :mod:`threading` module. -The :mod:`Queue` module defines the following class and exception: +Implements three types of queue whose only difference is the order that +the entries are retrieved. In a FIFO queue, the first tasks added are +the first retrieved. In a LIFO queue, the most recently added entry is +the first retrieved (operating like a stack). With a priority queue, +the entries are kept sorted (using the :mod:`heapq` module) and the +lowest valued entry is retrieved first. +The :mod:`Queue` module defines the following classes and exceptions: .. class:: Queue(maxsize) - Constructor for the class. *maxsize* is an integer that sets the upperbound + Constructor for a FIFO queue. *maxsize* is an integer that sets the upperbound limit on the number of items that can be placed in the queue. Insertion will block once this size has been reached, until queue items are consumed. If *maxsize* is less than or equal to zero, the queue size is infinite. +.. class:: LifoQueue(maxsize) + Constructor for a LIFO queue. *maxsize* is an integer that sets the upperbound + limit on the number of items that can be placed in the queue. Insertion will + block once this size has been reached, until queue items are consumed. If + *maxsize* is less than or equal to zero, the queue size is infinite. + +.. class:: PriorityQueue(maxsize) + + Constructor for a priority queue. *maxsize* is an integer that sets the upperbound + limit on the number of items that can be placed in the queue. Insertion will + block once this size has been reached, until queue items are consumed. If + *maxsize* is less than or equal to zero, the queue size is infinite. + + The lowest valued entries are retrieved first (the lowest valued entry is + one returned by ``sorted(list(entries))[0]``). A typical pattern for entries + is a tuple in the form: ``(priority_number, data)``. + .. exception:: Empty Exception raised when non-blocking :meth:`get` (or :meth:`get_nowait`) is called @@ -41,10 +64,8 @@ Queue Objects ------------- -Class :class:`Queue` implements queue objects and has the methods described -below. This class can be derived from in order to implement other queue -organizations (e.g. stack) but the inheritable interface is not described here. -See the source code for details. The public methods are: +Queue objects (:class:``Queue``, :class:``LifoQueue``, or :class:``PriorityQueue`` +provide the public methods described below. .. method:: Queue.qsize() Index: Lib/Queue.py =================================================================== --- Lib/Queue.py (revision 59990) +++ Lib/Queue.py (working copy) @@ -2,8 +2,9 @@ from time import time as _time from collections import deque +import heapq -__all__ = ['Empty', 'Full', 'Queue'] +__all__ = ['Empty', 'Full', 'Queue', 'PriorityQueue', 'LifoQueue'] class Empty(Exception): "Exception raised by Queue.get(block=0)/get_nowait()." @@ -206,3 +207,38 @@ # Get an item from the queue def _get(self): return self.queue.popleft() + + +class PriorityQueue(Queue): + '''Variant of Queue that retrieves open entries in priority order (lowest first). + + Entries are typically tuples of the form: (priority number, data). + ''' + + def _init(self, maxsize): + self.queue = [] + + def _qsize(self): + return len(self.queue) + + def _put(self, item, heappush=heapq.heappush): + heappush(self.queue, item) + + def _get(self, heappop=heapq.heappop): + return heappop(self.queue) + + +class LifoQueue(Queue): + '''Variant of Queue that retrieves most recented added entries first.''' + + def _init(self, maxsize): + self.queue = [] + + def _qsize(self): + return len(self.queue) + + def _put(self, item): + self.queue.append(item) + + def _get(self): + return self.queue.pop() Index: Lib/test/test_queue.py =================================================================== --- Lib/test/test_queue.py (revision 59990) +++ Lib/test/test_queue.py (working copy) @@ -181,8 +181,13 @@ raise RuntimeError, "Call this function with an empty queue" # I guess we better check things actually queue correctly a little :) q.put(111) + q.put(333) q.put(222) - verify(q.get() == 111 and q.get() == 222, + target_order = dict(Queue = [111, 333, 222], + LifoQueue = [222, 333, 111], + PriorityQueue = [111, 222, 333]) + actual_order = [q.get(), q.get(), q.get()] + verify(actual_order == target_order[q.__class__.__name__], "Didn't seem to queue the correct data!") for i in range(QUEUE_SIZE-1): q.put(i) @@ -260,18 +265,20 @@ raise TestFailed("Did not detect task count going negative") def test(): - q = Queue.Queue() - QueueTaskDoneTest(q) - QueueJoinTest(q) - QueueJoinTest(q) - QueueTaskDoneTest(q) + for Q in Queue.Queue, Queue.LifoQueue, Queue.PriorityQueue: + q = Q() + QueueTaskDoneTest(q) + QueueJoinTest(q) + QueueJoinTest(q) + QueueTaskDoneTest(q) - q = Queue.Queue(QUEUE_SIZE) - # Do it a couple of times on the same queue - SimpleQueueTest(q) - SimpleQueueTest(q) - if verbose: - print "Simple Queue tests seemed to work" + q = Q(QUEUE_SIZE) + # Do it a couple of times on the same queue + SimpleQueueTest(q) + SimpleQueueTest(q) + if verbose: + print "Simple Queue tests seemed to work for", Q.__name__ + q = FailingQueue(QUEUE_SIZE) FailingQueueTest(q) FailingQueueTest(q)