Raymond Hettinger | 0c5e52f | 2012-01-09 06:17:39 +0000 | [diff] [blame] | 1 | '''A multi-producer, multi-consumer queue.''' |
Guido van Rossum | 9022fce | 1992-08-25 12:30:44 +0000 | [diff] [blame] | 2 | |
Antoine Pitrou | a6a4dc8 | 2017-09-07 18:56:24 +0200 | [diff] [blame] | 3 | import threading |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 4 | from collections import deque |
Raymond Hettinger | 143f51a | 2012-01-09 05:32:01 +0000 | [diff] [blame] | 5 | from heapq import heappush, heappop |
Victor Stinner | ae58649 | 2014-09-02 23:18:25 +0200 | [diff] [blame] | 6 | from time import monotonic as time |
Martin v. Löwis | 77ac429 | 2002-10-15 15:11:13 +0000 | [diff] [blame] | 7 | |
Raymond Hettinger | 3564146 | 2008-01-17 00:13:27 +0000 | [diff] [blame] | 8 | __all__ = ['Empty', 'Full', 'Queue', 'PriorityQueue', 'LifoQueue'] |
Brett Cannon | b42bb5a | 2003-07-01 05:34:27 +0000 | [diff] [blame] | 9 | |
Tim Peters | b8c9417 | 2001-01-15 22:53:46 +0000 | [diff] [blame] | 10 | class Empty(Exception): |
Raymond Hettinger | 0c5e52f | 2012-01-09 06:17:39 +0000 | [diff] [blame] | 11 | 'Exception raised by Queue.get(block=0)/get_nowait().' |
Tim Peters | b8c9417 | 2001-01-15 22:53:46 +0000 | [diff] [blame] | 12 | pass |
| 13 | |
| 14 | class Full(Exception): |
Raymond Hettinger | 0c5e52f | 2012-01-09 06:17:39 +0000 | [diff] [blame] | 15 | 'Exception raised by Queue.put(block=0)/put_nowait().' |
Tim Peters | b8c9417 | 2001-01-15 22:53:46 +0000 | [diff] [blame] | 16 | pass |
Guido van Rossum | 9022fce | 1992-08-25 12:30:44 +0000 | [diff] [blame] | 17 | |
| 18 | class Queue: |
Raymond Hettinger | 0c5e52f | 2012-01-09 06:17:39 +0000 | [diff] [blame] | 19 | '''Create a queue object with a given maximum size. |
Guido van Rossum | 9022fce | 1992-08-25 12:30:44 +0000 | [diff] [blame] | 20 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 21 | If maxsize is <= 0, the queue size is infinite. |
Raymond Hettinger | 0c5e52f | 2012-01-09 06:17:39 +0000 | [diff] [blame] | 22 | ''' |
| 23 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 24 | def __init__(self, maxsize=0): |
Raymond Hettinger | da3caed | 2008-01-14 21:39:24 +0000 | [diff] [blame] | 25 | self.maxsize = maxsize |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 26 | self._init(maxsize) |
Raymond Hettinger | 7540427 | 2012-01-07 15:32:52 -0800 | [diff] [blame] | 27 | |
Tim Peters | 5af0e41 | 2004-07-12 00:45:14 +0000 | [diff] [blame] | 28 | # mutex must be held whenever the queue is mutating. All methods |
| 29 | # that acquire mutex must release it before returning. mutex |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 30 | # is shared between the three conditions, so acquiring and |
Tim Peters | 5af0e41 | 2004-07-12 00:45:14 +0000 | [diff] [blame] | 31 | # releasing the conditions also acquires and releases mutex. |
Raymond Hettinger | 143f51a | 2012-01-09 05:32:01 +0000 | [diff] [blame] | 32 | self.mutex = threading.Lock() |
Raymond Hettinger | 7540427 | 2012-01-07 15:32:52 -0800 | [diff] [blame] | 33 | |
Tim Peters | 5af0e41 | 2004-07-12 00:45:14 +0000 | [diff] [blame] | 34 | # Notify not_empty whenever an item is added to the queue; a |
| 35 | # thread waiting to get is notified then. |
Raymond Hettinger | 143f51a | 2012-01-09 05:32:01 +0000 | [diff] [blame] | 36 | self.not_empty = threading.Condition(self.mutex) |
Raymond Hettinger | 7540427 | 2012-01-07 15:32:52 -0800 | [diff] [blame] | 37 | |
Tim Peters | 5af0e41 | 2004-07-12 00:45:14 +0000 | [diff] [blame] | 38 | # Notify not_full whenever an item is removed from the queue; |
| 39 | # a thread waiting to put is notified then. |
Raymond Hettinger | 143f51a | 2012-01-09 05:32:01 +0000 | [diff] [blame] | 40 | self.not_full = threading.Condition(self.mutex) |
Raymond Hettinger | 7540427 | 2012-01-07 15:32:52 -0800 | [diff] [blame] | 41 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 42 | # Notify all_tasks_done whenever the number of unfinished tasks |
| 43 | # drops to zero; thread waiting to join() is notified to resume |
Raymond Hettinger | 143f51a | 2012-01-09 05:32:01 +0000 | [diff] [blame] | 44 | self.all_tasks_done = threading.Condition(self.mutex) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 45 | self.unfinished_tasks = 0 |
| 46 | |
| 47 | def task_done(self): |
Raymond Hettinger | 0c5e52f | 2012-01-09 06:17:39 +0000 | [diff] [blame] | 48 | '''Indicate that a formerly enqueued task is complete. |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 49 | |
| 50 | Used by Queue consumer threads. For each get() used to fetch a task, |
| 51 | a subsequent call to task_done() tells the queue that the processing |
| 52 | on the task is complete. |
| 53 | |
| 54 | If a join() is currently blocking, it will resume when all items |
| 55 | have been processed (meaning that a task_done() call was received |
| 56 | for every item that had been put() into the queue). |
| 57 | |
| 58 | Raises a ValueError if called more times than there were items |
| 59 | placed in the queue. |
Raymond Hettinger | 0c5e52f | 2012-01-09 06:17:39 +0000 | [diff] [blame] | 60 | ''' |
Raymond Hettinger | 7540427 | 2012-01-07 15:32:52 -0800 | [diff] [blame] | 61 | with self.all_tasks_done: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 62 | unfinished = self.unfinished_tasks - 1 |
| 63 | if unfinished <= 0: |
| 64 | if unfinished < 0: |
| 65 | raise ValueError('task_done() called too many times') |
Benjamin Peterson | 672b803 | 2008-06-11 19:14:14 +0000 | [diff] [blame] | 66 | self.all_tasks_done.notify_all() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 67 | self.unfinished_tasks = unfinished |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 68 | |
| 69 | def join(self): |
Raymond Hettinger | 0c5e52f | 2012-01-09 06:17:39 +0000 | [diff] [blame] | 70 | '''Blocks until all items in the Queue have been gotten and processed. |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 71 | |
| 72 | The count of unfinished tasks goes up whenever an item is added to the |
| 73 | queue. The count goes down whenever a consumer thread calls task_done() |
| 74 | to indicate the item was retrieved and all work on it is complete. |
| 75 | |
| 76 | When the count of unfinished tasks drops to zero, join() unblocks. |
Raymond Hettinger | 0c5e52f | 2012-01-09 06:17:39 +0000 | [diff] [blame] | 77 | ''' |
Raymond Hettinger | 7540427 | 2012-01-07 15:32:52 -0800 | [diff] [blame] | 78 | with self.all_tasks_done: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 79 | while self.unfinished_tasks: |
| 80 | self.all_tasks_done.wait() |
Guido van Rossum | 9022fce | 1992-08-25 12:30:44 +0000 | [diff] [blame] | 81 | |
Barry Warsaw | 3d96d52 | 1997-11-20 19:56:38 +0000 | [diff] [blame] | 82 | def qsize(self): |
Raymond Hettinger | 0c5e52f | 2012-01-09 06:17:39 +0000 | [diff] [blame] | 83 | '''Return the approximate size of the queue (not reliable!).''' |
Raymond Hettinger | 7540427 | 2012-01-07 15:32:52 -0800 | [diff] [blame] | 84 | with self.mutex: |
| 85 | return self._qsize() |
Barry Warsaw | 3d96d52 | 1997-11-20 19:56:38 +0000 | [diff] [blame] | 86 | |
Alexandre Vassalotti | f260e44 | 2008-05-11 19:59:59 +0000 | [diff] [blame] | 87 | def empty(self): |
Raymond Hettinger | 0c5e52f | 2012-01-09 06:17:39 +0000 | [diff] [blame] | 88 | '''Return True if the queue is empty, False otherwise (not reliable!). |
Raymond Hettinger | 611eaf0 | 2009-03-06 23:55:28 +0000 | [diff] [blame] | 89 | |
| 90 | This method is likely to be removed at some point. Use qsize() == 0 |
| 91 | as a direct substitute, but be aware that either approach risks a race |
| 92 | condition where a queue can grow before the result of empty() or |
| 93 | qsize() can be used. |
| 94 | |
| 95 | To create code that needs to wait for all queued tasks to be |
| 96 | completed, the preferred technique is to use the join() method. |
Raymond Hettinger | 0c5e52f | 2012-01-09 06:17:39 +0000 | [diff] [blame] | 97 | ''' |
Raymond Hettinger | 7540427 | 2012-01-07 15:32:52 -0800 | [diff] [blame] | 98 | with self.mutex: |
| 99 | return not self._qsize() |
Alexandre Vassalotti | f260e44 | 2008-05-11 19:59:59 +0000 | [diff] [blame] | 100 | |
| 101 | def full(self): |
Raymond Hettinger | 0c5e52f | 2012-01-09 06:17:39 +0000 | [diff] [blame] | 102 | '''Return True if the queue is full, False otherwise (not reliable!). |
Raymond Hettinger | 611eaf0 | 2009-03-06 23:55:28 +0000 | [diff] [blame] | 103 | |
Raymond Hettinger | 189316a | 2010-10-31 17:57:52 +0000 | [diff] [blame] | 104 | This method is likely to be removed at some point. Use qsize() >= n |
Raymond Hettinger | 611eaf0 | 2009-03-06 23:55:28 +0000 | [diff] [blame] | 105 | as a direct substitute, but be aware that either approach risks a race |
| 106 | condition where a queue can shrink before the result of full() or |
| 107 | qsize() can be used. |
Raymond Hettinger | 0c5e52f | 2012-01-09 06:17:39 +0000 | [diff] [blame] | 108 | ''' |
Raymond Hettinger | 7540427 | 2012-01-07 15:32:52 -0800 | [diff] [blame] | 109 | with self.mutex: |
| 110 | return 0 < self.maxsize <= self._qsize() |
Alexandre Vassalotti | f260e44 | 2008-05-11 19:59:59 +0000 | [diff] [blame] | 111 | |
Martin v. Löwis | 77ac429 | 2002-10-15 15:11:13 +0000 | [diff] [blame] | 112 | def put(self, item, block=True, timeout=None): |
Raymond Hettinger | 0c5e52f | 2012-01-09 06:17:39 +0000 | [diff] [blame] | 113 | '''Put an item into the queue. |
Guido van Rossum | c09e6b1 | 1998-04-09 14:25:32 +0000 | [diff] [blame] | 114 | |
Martin v. Löwis | 77ac429 | 2002-10-15 15:11:13 +0000 | [diff] [blame] | 115 | If optional args 'block' is true and 'timeout' is None (the default), |
| 116 | block if necessary until a free slot is available. If 'timeout' is |
Terry Jan Reedy | 7608b60 | 2013-08-10 18:17:13 -0400 | [diff] [blame] | 117 | a non-negative number, it blocks at most 'timeout' seconds and raises |
Martin v. Löwis | 77ac429 | 2002-10-15 15:11:13 +0000 | [diff] [blame] | 118 | the Full exception if no free slot was available within that time. |
| 119 | Otherwise ('block' is false), put an item on the queue if a free slot |
| 120 | is immediately available, else raise the Full exception ('timeout' |
| 121 | is ignored in that case). |
Raymond Hettinger | 0c5e52f | 2012-01-09 06:17:39 +0000 | [diff] [blame] | 122 | ''' |
Raymond Hettinger | 7540427 | 2012-01-07 15:32:52 -0800 | [diff] [blame] | 123 | with self.not_full: |
Raymond Hettinger | ae138cb | 2008-01-15 20:42:00 +0000 | [diff] [blame] | 124 | if self.maxsize > 0: |
| 125 | if not block: |
Raymond Hettinger | 189316a | 2010-10-31 17:57:52 +0000 | [diff] [blame] | 126 | if self._qsize() >= self.maxsize: |
Raymond Hettinger | ae138cb | 2008-01-15 20:42:00 +0000 | [diff] [blame] | 127 | raise Full |
| 128 | elif timeout is None: |
Raymond Hettinger | 189316a | 2010-10-31 17:57:52 +0000 | [diff] [blame] | 129 | while self._qsize() >= self.maxsize: |
Raymond Hettinger | da3caed | 2008-01-14 21:39:24 +0000 | [diff] [blame] | 130 | self.not_full.wait() |
Raymond Hettinger | ae138cb | 2008-01-15 20:42:00 +0000 | [diff] [blame] | 131 | elif timeout < 0: |
Terry Jan Reedy | 7608b60 | 2013-08-10 18:17:13 -0400 | [diff] [blame] | 132 | raise ValueError("'timeout' must be a non-negative number") |
Raymond Hettinger | ae138cb | 2008-01-15 20:42:00 +0000 | [diff] [blame] | 133 | else: |
Raymond Hettinger | 143f51a | 2012-01-09 05:32:01 +0000 | [diff] [blame] | 134 | endtime = time() + timeout |
Raymond Hettinger | 189316a | 2010-10-31 17:57:52 +0000 | [diff] [blame] | 135 | while self._qsize() >= self.maxsize: |
Raymond Hettinger | 143f51a | 2012-01-09 05:32:01 +0000 | [diff] [blame] | 136 | remaining = endtime - time() |
Raymond Hettinger | da3caed | 2008-01-14 21:39:24 +0000 | [diff] [blame] | 137 | if remaining <= 0.0: |
| 138 | raise Full |
| 139 | self.not_full.wait(remaining) |
Mark Hammond | 3b959db | 2002-04-19 00:11:32 +0000 | [diff] [blame] | 140 | self._put(item) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 141 | self.unfinished_tasks += 1 |
Tim Peters | 5af0e41 | 2004-07-12 00:45:14 +0000 | [diff] [blame] | 142 | self.not_empty.notify() |
Barry Warsaw | 3d96d52 | 1997-11-20 19:56:38 +0000 | [diff] [blame] | 143 | |
Martin v. Löwis | 77ac429 | 2002-10-15 15:11:13 +0000 | [diff] [blame] | 144 | def get(self, block=True, timeout=None): |
Raymond Hettinger | 0c5e52f | 2012-01-09 06:17:39 +0000 | [diff] [blame] | 145 | '''Remove and return an item from the queue. |
Guido van Rossum | c09e6b1 | 1998-04-09 14:25:32 +0000 | [diff] [blame] | 146 | |
Martin v. Löwis | 77ac429 | 2002-10-15 15:11:13 +0000 | [diff] [blame] | 147 | If optional args 'block' is true and 'timeout' is None (the default), |
| 148 | block if necessary until an item is available. If 'timeout' is |
Terry Jan Reedy | 7608b60 | 2013-08-10 18:17:13 -0400 | [diff] [blame] | 149 | a non-negative number, it blocks at most 'timeout' seconds and raises |
Martin v. Löwis | 77ac429 | 2002-10-15 15:11:13 +0000 | [diff] [blame] | 150 | the Empty exception if no item was available within that time. |
| 151 | Otherwise ('block' is false), return an item if one is immediately |
| 152 | available, else raise the Empty exception ('timeout' is ignored |
| 153 | in that case). |
Raymond Hettinger | 0c5e52f | 2012-01-09 06:17:39 +0000 | [diff] [blame] | 154 | ''' |
Raymond Hettinger | 7540427 | 2012-01-07 15:32:52 -0800 | [diff] [blame] | 155 | with self.not_empty: |
Tim Peters | 71ed220 | 2004-07-12 01:20:32 +0000 | [diff] [blame] | 156 | if not block: |
Raymond Hettinger | da3caed | 2008-01-14 21:39:24 +0000 | [diff] [blame] | 157 | if not self._qsize(): |
Tim Peters | 71ed220 | 2004-07-12 01:20:32 +0000 | [diff] [blame] | 158 | raise Empty |
| 159 | elif timeout is None: |
Raymond Hettinger | da3caed | 2008-01-14 21:39:24 +0000 | [diff] [blame] | 160 | while not self._qsize(): |
Tim Peters | 5af0e41 | 2004-07-12 00:45:14 +0000 | [diff] [blame] | 161 | self.not_empty.wait() |
Raymond Hettinger | ae138cb | 2008-01-15 20:42:00 +0000 | [diff] [blame] | 162 | elif timeout < 0: |
Terry Jan Reedy | 7608b60 | 2013-08-10 18:17:13 -0400 | [diff] [blame] | 163 | raise ValueError("'timeout' must be a non-negative number") |
Tim Peters | 5af0e41 | 2004-07-12 00:45:14 +0000 | [diff] [blame] | 164 | else: |
Raymond Hettinger | 143f51a | 2012-01-09 05:32:01 +0000 | [diff] [blame] | 165 | endtime = time() + timeout |
Raymond Hettinger | da3caed | 2008-01-14 21:39:24 +0000 | [diff] [blame] | 166 | while not self._qsize(): |
Raymond Hettinger | 143f51a | 2012-01-09 05:32:01 +0000 | [diff] [blame] | 167 | remaining = endtime - time() |
Tim Peters | 71ed220 | 2004-07-12 01:20:32 +0000 | [diff] [blame] | 168 | if remaining <= 0.0: |
Tim Peters | 5af0e41 | 2004-07-12 00:45:14 +0000 | [diff] [blame] | 169 | raise Empty |
| 170 | self.not_empty.wait(remaining) |
Mark Hammond | 3b959db | 2002-04-19 00:11:32 +0000 | [diff] [blame] | 171 | item = self._get() |
Tim Peters | 5af0e41 | 2004-07-12 00:45:14 +0000 | [diff] [blame] | 172 | self.not_full.notify() |
| 173 | return item |
Guido van Rossum | 9022fce | 1992-08-25 12:30:44 +0000 | [diff] [blame] | 174 | |
Raymond Hettinger | 61bd729 | 2012-01-09 06:02:08 +0000 | [diff] [blame] | 175 | def put_nowait(self, item): |
Raymond Hettinger | 0c5e52f | 2012-01-09 06:17:39 +0000 | [diff] [blame] | 176 | '''Put an item into the queue without blocking. |
Raymond Hettinger | 61bd729 | 2012-01-09 06:02:08 +0000 | [diff] [blame] | 177 | |
| 178 | Only enqueue the item if a free slot is immediately available. |
| 179 | Otherwise raise the Full exception. |
Raymond Hettinger | 0c5e52f | 2012-01-09 06:17:39 +0000 | [diff] [blame] | 180 | ''' |
Raymond Hettinger | 61bd729 | 2012-01-09 06:02:08 +0000 | [diff] [blame] | 181 | return self.put(item, block=False) |
| 182 | |
Guido van Rossum | 9e1721f | 1999-02-08 18:34:01 +0000 | [diff] [blame] | 183 | def get_nowait(self): |
Raymond Hettinger | 0c5e52f | 2012-01-09 06:17:39 +0000 | [diff] [blame] | 184 | '''Remove and return an item from the queue without blocking. |
Guido van Rossum | 9022fce | 1992-08-25 12:30:44 +0000 | [diff] [blame] | 185 | |
Martin v. Löwis | 77ac429 | 2002-10-15 15:11:13 +0000 | [diff] [blame] | 186 | Only get an item if one is immediately available. Otherwise |
Guido van Rossum | 9e1721f | 1999-02-08 18:34:01 +0000 | [diff] [blame] | 187 | raise the Empty exception. |
Raymond Hettinger | 0c5e52f | 2012-01-09 06:17:39 +0000 | [diff] [blame] | 188 | ''' |
Raymond Hettinger | 61bd729 | 2012-01-09 06:02:08 +0000 | [diff] [blame] | 189 | return self.get(block=False) |
Guido van Rossum | 9022fce | 1992-08-25 12:30:44 +0000 | [diff] [blame] | 190 | |
Barry Warsaw | 3d96d52 | 1997-11-20 19:56:38 +0000 | [diff] [blame] | 191 | # Override these methods to implement other queue organizations |
| 192 | # (e.g. stack or priority queue). |
| 193 | # These will only be called with appropriate locks held |
Guido van Rossum | 9022fce | 1992-08-25 12:30:44 +0000 | [diff] [blame] | 194 | |
Barry Warsaw | 3d96d52 | 1997-11-20 19:56:38 +0000 | [diff] [blame] | 195 | # Initialize the queue representation |
| 196 | def _init(self, maxsize): |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 197 | self.queue = deque() |
Guido van Rossum | 9022fce | 1992-08-25 12:30:44 +0000 | [diff] [blame] | 198 | |
Raymond Hettinger | 143f51a | 2012-01-09 05:32:01 +0000 | [diff] [blame] | 199 | def _qsize(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 200 | return len(self.queue) |
Guido van Rossum | 9022fce | 1992-08-25 12:30:44 +0000 | [diff] [blame] | 201 | |
Barry Warsaw | 3d96d52 | 1997-11-20 19:56:38 +0000 | [diff] [blame] | 202 | # Put a new item in the queue |
| 203 | def _put(self, item): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 204 | self.queue.append(item) |
Guido van Rossum | 9022fce | 1992-08-25 12:30:44 +0000 | [diff] [blame] | 205 | |
Barry Warsaw | 3d96d52 | 1997-11-20 19:56:38 +0000 | [diff] [blame] | 206 | # Get an item from the queue |
| 207 | def _get(self): |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 208 | return self.queue.popleft() |
Raymond Hettinger | 3564146 | 2008-01-17 00:13:27 +0000 | [diff] [blame] | 209 | |
| 210 | |
| 211 | class PriorityQueue(Queue): |
| 212 | '''Variant of Queue that retrieves open entries in priority order (lowest first). |
| 213 | |
| 214 | Entries are typically tuples of the form: (priority number, data). |
| 215 | ''' |
| 216 | |
| 217 | def _init(self, maxsize): |
| 218 | self.queue = [] |
| 219 | |
Raymond Hettinger | 143f51a | 2012-01-09 05:32:01 +0000 | [diff] [blame] | 220 | def _qsize(self): |
Raymond Hettinger | 3564146 | 2008-01-17 00:13:27 +0000 | [diff] [blame] | 221 | return len(self.queue) |
| 222 | |
Raymond Hettinger | 143f51a | 2012-01-09 05:32:01 +0000 | [diff] [blame] | 223 | def _put(self, item): |
Raymond Hettinger | 3564146 | 2008-01-17 00:13:27 +0000 | [diff] [blame] | 224 | heappush(self.queue, item) |
| 225 | |
Raymond Hettinger | 143f51a | 2012-01-09 05:32:01 +0000 | [diff] [blame] | 226 | def _get(self): |
Raymond Hettinger | 3564146 | 2008-01-17 00:13:27 +0000 | [diff] [blame] | 227 | return heappop(self.queue) |
| 228 | |
| 229 | |
| 230 | class LifoQueue(Queue): |
| 231 | '''Variant of Queue that retrieves most recently added entries first.''' |
| 232 | |
| 233 | def _init(self, maxsize): |
| 234 | self.queue = [] |
| 235 | |
Raymond Hettinger | 143f51a | 2012-01-09 05:32:01 +0000 | [diff] [blame] | 236 | def _qsize(self): |
Raymond Hettinger | 3564146 | 2008-01-17 00:13:27 +0000 | [diff] [blame] | 237 | return len(self.queue) |
| 238 | |
| 239 | def _put(self, item): |
| 240 | self.queue.append(item) |
| 241 | |
| 242 | def _get(self): |
| 243 | return self.queue.pop() |