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