Alexandre Vassalotti | f260e44 | 2008-05-11 19:59:59 +0000 | [diff] [blame] | 1 | :mod:`queue` --- A synchronized queue class |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2 | =========================================== |
| 3 | |
Alexandre Vassalotti | f260e44 | 2008-05-11 19:59:59 +0000 | [diff] [blame] | 4 | .. module:: queue |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 5 | :synopsis: A synchronized queue class. |
| 6 | |
Raymond Hettinger | 1048094 | 2011-01-10 03:26:08 +0000 | [diff] [blame] | 7 | **Source code:** :source:`Lib/queue.py` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 8 | |
Raymond Hettinger | 4f707fd | 2011-01-10 19:54:11 +0000 | [diff] [blame] | 9 | -------------- |
| 10 | |
Alexandre Vassalotti | f260e44 | 2008-05-11 19:59:59 +0000 | [diff] [blame] | 11 | The :mod:`queue` module implements multi-producer, multi-consumer queues. |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 12 | It is especially useful in threaded programming when information must be |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 13 | exchanged safely between multiple threads. The :class:`Queue` class in this |
| 14 | module implements all the required locking semantics. It depends on the |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 15 | availability of thread support in Python; see the :mod:`threading` |
| 16 | module. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 17 | |
R David Murray | b98b37f | 2012-05-08 21:28:24 -0400 | [diff] [blame] | 18 | The module implements three types of queue, which differ only in the order in |
Serhiy Storchaka | 4ecfa45 | 2016-05-16 09:31:54 +0300 | [diff] [blame] | 19 | which the entries are retrieved. In a :abbr:`FIFO (first-in, first-out)` |
| 20 | queue, the first tasks added are the first retrieved. In a |
| 21 | :abbr:`LIFO (last-in, first-out)` queue, the most recently added entry is |
Raymond Hettinger | 3564146 | 2008-01-17 00:13:27 +0000 | [diff] [blame] | 22 | the first retrieved (operating like a stack). With a priority queue, |
| 23 | the entries are kept sorted (using the :mod:`heapq` module) and the |
| 24 | lowest valued entry is retrieved first. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 25 | |
Raymond Hettinger | 87dc4d6 | 2016-09-09 15:57:13 -0700 | [diff] [blame] | 26 | Internally, the module uses locks to temporarily block competing threads; |
| 27 | however, it is not designed to handle reentrancy within a thread. |
Éric Araujo | 6e6cb8e | 2010-11-16 19:13:50 +0000 | [diff] [blame] | 28 | |
Alexandre Vassalotti | f260e44 | 2008-05-11 19:59:59 +0000 | [diff] [blame] | 29 | The :mod:`queue` module defines the following classes and exceptions: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 30 | |
Andrew M. Kuchling | 2b600e5 | 2010-02-26 13:35:56 +0000 | [diff] [blame] | 31 | .. class:: Queue(maxsize=0) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 32 | |
Serhiy Storchaka | 4ecfa45 | 2016-05-16 09:31:54 +0300 | [diff] [blame] | 33 | Constructor for a :abbr:`FIFO (first-in, first-out)` queue. *maxsize* is |
| 34 | an integer that sets the upperbound |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 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 | |
Andrew M. Kuchling | 2b600e5 | 2010-02-26 13:35:56 +0000 | [diff] [blame] | 39 | .. class:: LifoQueue(maxsize=0) |
Raymond Hettinger | 3564146 | 2008-01-17 00:13:27 +0000 | [diff] [blame] | 40 | |
Serhiy Storchaka | 4ecfa45 | 2016-05-16 09:31:54 +0300 | [diff] [blame] | 41 | Constructor for a :abbr:`LIFO (last-in, first-out)` queue. *maxsize* is |
| 42 | an integer that sets the upperbound |
Raymond Hettinger | 3564146 | 2008-01-17 00:13:27 +0000 | [diff] [blame] | 43 | limit on the number of items that can be placed in the queue. Insertion will |
| 44 | block once this size has been reached, until queue items are consumed. If |
| 45 | *maxsize* is less than or equal to zero, the queue size is infinite. |
| 46 | |
Christian Heimes | 679db4a | 2008-01-18 09:56:22 +0000 | [diff] [blame] | 47 | |
Andrew M. Kuchling | 2b600e5 | 2010-02-26 13:35:56 +0000 | [diff] [blame] | 48 | .. class:: PriorityQueue(maxsize=0) |
Raymond Hettinger | 3564146 | 2008-01-17 00:13:27 +0000 | [diff] [blame] | 49 | |
| 50 | Constructor for a priority queue. *maxsize* is an integer that sets the upperbound |
| 51 | limit on the number of items that can be placed in the queue. Insertion will |
| 52 | block once this size has been reached, until queue items are consumed. If |
| 53 | *maxsize* is less than or equal to zero, the queue size is infinite. |
| 54 | |
| 55 | The lowest valued entries are retrieved first (the lowest valued entry is the |
| 56 | one returned by ``sorted(list(entries))[0]``). A typical pattern for entries |
| 57 | is a tuple in the form: ``(priority_number, data)``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 58 | |
Christian Heimes | 679db4a | 2008-01-18 09:56:22 +0000 | [diff] [blame] | 59 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 60 | .. exception:: Empty |
| 61 | |
Serhiy Storchaka | 9e0ae53 | 2013-08-24 00:23:38 +0300 | [diff] [blame] | 62 | Exception raised when non-blocking :meth:`~Queue.get` (or |
| 63 | :meth:`~Queue.get_nowait`) is called |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 64 | on a :class:`Queue` object which is empty. |
| 65 | |
| 66 | |
| 67 | .. exception:: Full |
| 68 | |
Serhiy Storchaka | 9e0ae53 | 2013-08-24 00:23:38 +0300 | [diff] [blame] | 69 | Exception raised when non-blocking :meth:`~Queue.put` (or |
| 70 | :meth:`~Queue.put_nowait`) is called |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 71 | on a :class:`Queue` object which is full. |
| 72 | |
| 73 | |
| 74 | .. _queueobjects: |
| 75 | |
| 76 | Queue Objects |
| 77 | ------------- |
| 78 | |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 79 | Queue objects (:class:`Queue`, :class:`LifoQueue`, or :class:`PriorityQueue`) |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 80 | provide the public methods described below. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 81 | |
| 82 | |
| 83 | .. method:: Queue.qsize() |
| 84 | |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 85 | Return the approximate size of the queue. Note, qsize() > 0 doesn't |
| 86 | guarantee that a subsequent get() will not block, nor will qsize() < maxsize |
| 87 | guarantee that put() will not block. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 88 | |
| 89 | |
Raymond Hettinger | 47aa989 | 2009-03-07 14:07:37 +0000 | [diff] [blame] | 90 | .. method:: Queue.empty() |
| 91 | |
| 92 | Return ``True`` if the queue is empty, ``False`` otherwise. If empty() |
| 93 | returns ``True`` it doesn't guarantee that a subsequent call to put() |
| 94 | will not block. Similarly, if empty() returns ``False`` it doesn't |
| 95 | guarantee that a subsequent call to get() will not block. |
| 96 | |
| 97 | |
| 98 | .. method:: Queue.full() |
| 99 | |
| 100 | Return ``True`` if the queue is full, ``False`` otherwise. If full() |
| 101 | returns ``True`` it doesn't guarantee that a subsequent call to get() |
| 102 | will not block. Similarly, if full() returns ``False`` it doesn't |
| 103 | guarantee that a subsequent call to put() will not block. |
| 104 | |
| 105 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 106 | .. method:: Queue.put(item, block=True, timeout=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 107 | |
| 108 | Put *item* into the queue. If optional args *block* is true and *timeout* is |
Serhiy Storchaka | ecf41da | 2016-10-19 16:29:26 +0300 | [diff] [blame] | 109 | ``None`` (the default), block if necessary until a free slot is available. If |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 110 | *timeout* is a positive number, it blocks at most *timeout* seconds and raises |
| 111 | the :exc:`Full` exception if no free slot was available within that time. |
| 112 | Otherwise (*block* is false), put an item on the queue if a free slot is |
| 113 | immediately available, else raise the :exc:`Full` exception (*timeout* is |
| 114 | ignored in that case). |
| 115 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 116 | |
| 117 | .. method:: Queue.put_nowait(item) |
| 118 | |
| 119 | Equivalent to ``put(item, False)``. |
| 120 | |
| 121 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 122 | .. method:: Queue.get(block=True, timeout=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 123 | |
| 124 | Remove and return an item from the queue. If optional args *block* is true and |
Serhiy Storchaka | ecf41da | 2016-10-19 16:29:26 +0300 | [diff] [blame] | 125 | *timeout* is ``None`` (the default), block if necessary until an item is available. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 126 | If *timeout* is a positive number, it blocks at most *timeout* seconds and |
| 127 | raises the :exc:`Empty` exception if no item was available within that time. |
| 128 | Otherwise (*block* is false), return an item if one is immediately available, |
| 129 | else raise the :exc:`Empty` exception (*timeout* is ignored in that case). |
| 130 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 131 | |
| 132 | .. method:: Queue.get_nowait() |
| 133 | |
| 134 | Equivalent to ``get(False)``. |
| 135 | |
| 136 | Two methods are offered to support tracking whether enqueued tasks have been |
| 137 | fully processed by daemon consumer threads. |
| 138 | |
| 139 | |
| 140 | .. method:: Queue.task_done() |
| 141 | |
| 142 | Indicate that a formerly enqueued task is complete. Used by queue consumer |
| 143 | threads. For each :meth:`get` used to fetch a task, a subsequent call to |
| 144 | :meth:`task_done` tells the queue that the processing on the task is complete. |
| 145 | |
| 146 | If a :meth:`join` is currently blocking, it will resume when all items have been |
| 147 | processed (meaning that a :meth:`task_done` call was received for every item |
| 148 | that had been :meth:`put` into the queue). |
| 149 | |
| 150 | Raises a :exc:`ValueError` if called more times than there were items placed in |
| 151 | the queue. |
| 152 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 153 | |
| 154 | .. method:: Queue.join() |
| 155 | |
| 156 | Blocks until all items in the queue have been gotten and processed. |
| 157 | |
| 158 | The count of unfinished tasks goes up whenever an item is added to the queue. |
| 159 | The count goes down whenever a consumer thread calls :meth:`task_done` to |
| 160 | indicate that the item was retrieved and all work on it is complete. When the |
Raymond Hettinger | 28c013d | 2009-03-10 00:07:25 +0000 | [diff] [blame] | 161 | count of unfinished tasks drops to zero, :meth:`join` unblocks. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 162 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 163 | |
| 164 | Example of how to wait for enqueued tasks to be completed:: |
| 165 | |
Victor Stinner | de31134 | 2015-03-18 14:05:43 +0100 | [diff] [blame] | 166 | def worker(): |
| 167 | while True: |
| 168 | item = q.get() |
| 169 | if item is None: |
| 170 | break |
| 171 | do_work(item) |
| 172 | q.task_done() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 173 | |
Victor Stinner | de31134 | 2015-03-18 14:05:43 +0100 | [diff] [blame] | 174 | q = queue.Queue() |
| 175 | threads = [] |
| 176 | for i in range(num_worker_threads): |
| 177 | t = threading.Thread(target=worker) |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 178 | t.start() |
Victor Stinner | de31134 | 2015-03-18 14:05:43 +0100 | [diff] [blame] | 179 | threads.append(t) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 180 | |
Victor Stinner | de31134 | 2015-03-18 14:05:43 +0100 | [diff] [blame] | 181 | for item in source(): |
| 182 | q.put(item) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 183 | |
Victor Stinner | de31134 | 2015-03-18 14:05:43 +0100 | [diff] [blame] | 184 | # block until all tasks are done |
| 185 | q.join() |
| 186 | |
| 187 | # stop workers |
| 188 | for i in range(num_worker_threads): |
| 189 | q.put(None) |
| 190 | for t in threads: |
| 191 | t.join() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 192 | |
Antoine Pitrou | 696efdd | 2011-01-07 19:16:12 +0000 | [diff] [blame] | 193 | |
| 194 | .. seealso:: |
| 195 | |
| 196 | Class :class:`multiprocessing.Queue` |
| 197 | A queue class for use in a multi-processing (rather than multi-threading) |
| 198 | context. |
| 199 | |
Georg Brandl | 2f2a9f7 | 2011-01-07 20:58:25 +0000 | [diff] [blame] | 200 | :class:`collections.deque` is an alternative implementation of unbounded |
Serhiy Storchaka | 9e0ae53 | 2013-08-24 00:23:38 +0300 | [diff] [blame] | 201 | queues with fast atomic :meth:`~collections.deque.append` and |
| 202 | :meth:`~collections.deque.popleft` operations that do not require locking. |
Raymond Hettinger | fc90213 | 2011-01-07 20:33:09 +0000 | [diff] [blame] | 203 | |