Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3564146

Browse files
committed
Sync-up Queue.py with Py2.6
1 parent bae82b8 commit 3564146

2 files changed

Lines changed: 66 additions & 9 deletions

File tree

Doc/library/queue.rst

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,46 @@
66
:synopsis: A synchronized queue class.
77

88

9-
The :mod:`Queue` module implements a multi-producer, multi-consumer FIFO queue.
9+
The :mod:`Queue` module implements multi-producer, multi-consumer queues.
1010
It is especially useful in threaded programming when information must be
1111
exchanged safely between multiple threads. The :class:`Queue` class in this
1212
module implements all the required locking semantics. It depends on the
1313
availability of thread support in Python; see the :mod:`threading`
1414
module.
1515

16-
The :mod:`Queue` module defines the following class and exception:
16+
Implements three types of queue whose only difference is the order that
17+
the entries are retrieved. In a FIFO queue, the first tasks added are
18+
the first retrieved. In a LIFO queue, the most recently added entry is
19+
the first retrieved (operating like a stack). With a priority queue,
20+
the entries are kept sorted (using the :mod:`heapq` module) and the
21+
lowest valued entry is retrieved first.
1722

23+
The :mod:`Queue` module defines the following classes and exceptions:
1824

1925
.. class:: Queue(maxsize)
2026

21-
Constructor for the class. *maxsize* is an integer that sets the upperbound
27+
Constructor for a FIFO queue. *maxsize* is an integer that sets the upperbound
2228
limit on the number of items that can be placed in the queue. Insertion will
2329
block once this size has been reached, until queue items are consumed. If
2430
*maxsize* is less than or equal to zero, the queue size is infinite.
2531

32+
.. class:: LifoQueue(maxsize)
33+
34+
Constructor for a LIFO queue. *maxsize* is an integer that sets the upperbound
35+
limit on the number of items that can be placed in the queue. Insertion will
36+
block once this size has been reached, until queue items are consumed. If
37+
*maxsize* is less than or equal to zero, the queue size is infinite.
38+
39+
.. class:: PriorityQueue(maxsize)
40+
41+
Constructor for a priority queue. *maxsize* is an integer that sets the upperbound
42+
limit on the number of items that can be placed in the queue. Insertion will
43+
block once this size has been reached, until queue items are consumed. If
44+
*maxsize* is less than or equal to zero, the queue size is infinite.
45+
46+
The lowest valued entries are retrieved first (the lowest valued entry is the
47+
one returned by ``sorted(list(entries))[0]``). A typical pattern for entries
48+
is a tuple in the form: ``(priority_number, data)``.
2649

2750
.. exception:: Empty
2851

@@ -41,10 +64,8 @@ The :mod:`Queue` module defines the following class and exception:
4164
Queue Objects
4265
-------------
4366

44-
Class :class:`Queue` implements queue objects and has the methods described
45-
below. This class can be derived from in order to implement other queue
46-
organizations (e.g. stack) but the inheritable interface is not described here.
47-
See the source code for details. The public methods are:
67+
Queue objects (:class:``Queue``, :class:``LifoQueue``, or :class:``PriorityQueue``
68+
provide the public methods described below.
4869

4970

5071
.. method:: Queue.qsize()

Lib/Queue.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
from time import time as _time
44
from collections import deque
5+
import heapq
56

6-
__all__ = ['Empty', 'Full', 'Queue']
7+
__all__ = ['Empty', 'Full', 'Queue', 'PriorityQueue', 'LifoQueue']
78

89
class Empty(Exception):
910
"Exception raised by Queue.get(block=0)/get_nowait()."
@@ -182,7 +183,7 @@ def get_nowait(self):
182183
def _init(self, maxsize):
183184
self.queue = deque()
184185

185-
def _qsize(self):
186+
def _qsize(self, len=len):
186187
return len(self.queue)
187188

188189
# Put a new item in the queue
@@ -192,3 +193,38 @@ def _put(self, item):
192193
# Get an item from the queue
193194
def _get(self):
194195
return self.queue.popleft()
196+
197+
198+
class PriorityQueue(Queue):
199+
'''Variant of Queue that retrieves open entries in priority order (lowest first).
200+
201+
Entries are typically tuples of the form: (priority number, data).
202+
'''
203+
204+
def _init(self, maxsize):
205+
self.queue = []
206+
207+
def _qsize(self, len=len):
208+
return len(self.queue)
209+
210+
def _put(self, item, heappush=heapq.heappush):
211+
heappush(self.queue, item)
212+
213+
def _get(self, heappop=heapq.heappop):
214+
return heappop(self.queue)
215+
216+
217+
class LifoQueue(Queue):
218+
'''Variant of Queue that retrieves most recently added entries first.'''
219+
220+
def _init(self, maxsize):
221+
self.queue = []
222+
223+
def _qsize(self, len=len):
224+
return len(self.queue)
225+
226+
def _put(self, item):
227+
self.queue.append(item)
228+
229+
def _get(self):
230+
return self.queue.pop()

0 commit comments

Comments
 (0)