diff options
author | Raymond Hettinger <python@rcn.com> | 2008-01-17 00:13:27 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2008-01-17 00:13:27 (GMT) |
commit | 35641461db7c692877ffa4bbbfe31a525c81770e (patch) | |
tree | c477e4a7c582ab7302533e0401dc0adc07e272a5 /Lib/Queue.py | |
parent | bae82b8ae55b081533abfb9a387c04a04f3ff6b6 (diff) | |
download | cpython-35641461db7c692877ffa4bbbfe31a525c81770e.zip cpython-35641461db7c692877ffa4bbbfe31a525c81770e.tar.gz cpython-35641461db7c692877ffa4bbbfe31a525c81770e.tar.bz2 |
Sync-up Queue.py with Py2.6
Diffstat (limited to 'Lib/Queue.py')
-rw-r--r-- | Lib/Queue.py | 40 |
1 files changed, 38 insertions, 2 deletions
diff --git a/Lib/Queue.py b/Lib/Queue.py index f8aa0af..4d89d97 100644 --- a/Lib/Queue.py +++ b/Lib/Queue.py @@ -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()." @@ -182,7 +183,7 @@ class Queue: def _init(self, maxsize): self.queue = deque() - def _qsize(self): + def _qsize(self, len=len): return len(self.queue) # Put a new item in the queue @@ -192,3 +193,38 @@ class Queue: # 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, len=len): + 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 recently added entries first.''' + + def _init(self, maxsize): + self.queue = [] + + def _qsize(self, len=len): + return len(self.queue) + + def _put(self, item): + self.queue.append(item) + + def _get(self): + return self.queue.pop() |