Georg Brandl | 7a148c2 | 2008-05-12 10:03:16 +0000 | [diff] [blame] | 1 | :mod:`queue` --- A synchronized queue class |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 2 | =========================================== |
| 3 | |
| 4 | .. module:: Queue |
| 5 | :synopsis: A synchronized queue class. |
| 6 | |
Alexandre Vassalotti | 73812bf | 2008-05-11 20:04:03 +0000 | [diff] [blame] | 7 | .. note:: |
Georg Brandl | a6168f9 | 2008-05-25 07:20:14 +0000 | [diff] [blame] | 8 | The :mod:`Queue` module has been renamed to :mod:`queue` in Python 3.0. The |
| 9 | :term:`2to3` tool will automatically adapt imports when converting your |
| 10 | sources to 3.0. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 11 | |
Georg Brandl | 7a148c2 | 2008-05-12 10:03:16 +0000 | [diff] [blame] | 12 | |
Georg Brandl | a6168f9 | 2008-05-25 07:20:14 +0000 | [diff] [blame] | 13 | The :mod:`Queue` module implements multi-producer, multi-consumer queues. |
Mark Summerfield | fcb444a | 2007-09-04 08:16:15 +0000 | [diff] [blame] | 14 | It is especially useful in threaded programming when information must be |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 15 | exchanged safely between multiple threads. The :class:`Queue` class in this |
| 16 | module implements all the required locking semantics. It depends on the |
Mark Summerfield | fcb444a | 2007-09-04 08:16:15 +0000 | [diff] [blame] | 17 | availability of thread support in Python; see the :mod:`threading` |
| 18 | module. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 19 | |
Raymond Hettinger | 171f391 | 2008-01-16 23:38:16 +0000 | [diff] [blame] | 20 | Implements three types of queue whose only difference is the order that |
| 21 | the entries are retrieved. In a FIFO queue, the first tasks added are |
| 22 | the first retrieved. In a LIFO queue, the most recently added entry is |
| 23 | the first retrieved (operating like a stack). With a priority queue, |
| 24 | the entries are kept sorted (using the :mod:`heapq` module) and the |
| 25 | lowest valued entry is retrieved first. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 26 | |
Georg Brandl | a6168f9 | 2008-05-25 07:20:14 +0000 | [diff] [blame] | 27 | The :mod:`Queue` module defines the following classes and exceptions: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 28 | |
Andrew M. Kuchling | 9aeeffa | 2010-02-26 13:22:50 +0000 | [diff] [blame^] | 29 | .. class:: Queue(maxsize=0) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 30 | |
Raymond Hettinger | 171f391 | 2008-01-16 23:38:16 +0000 | [diff] [blame] | 31 | Constructor for a FIFO queue. *maxsize* is an integer that sets the upperbound |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 32 | limit on the number of items that can be placed in the queue. Insertion will |
| 33 | block once this size has been reached, until queue items are consumed. If |
| 34 | *maxsize* is less than or equal to zero, the queue size is infinite. |
| 35 | |
Andrew M. Kuchling | 9aeeffa | 2010-02-26 13:22:50 +0000 | [diff] [blame^] | 36 | .. class:: LifoQueue(maxsize=0) |
Raymond Hettinger | 171f391 | 2008-01-16 23:38:16 +0000 | [diff] [blame] | 37 | |
| 38 | Constructor for a LIFO queue. *maxsize* is an integer that sets the upperbound |
| 39 | limit on the number of items that can be placed in the queue. Insertion will |
| 40 | block once this size has been reached, until queue items are consumed. If |
| 41 | *maxsize* is less than or equal to zero, the queue size is infinite. |
| 42 | |
Raymond Hettinger | d59f457 | 2008-01-17 08:07:05 +0000 | [diff] [blame] | 43 | .. versionadded:: 2.6 |
| 44 | |
Andrew M. Kuchling | 9aeeffa | 2010-02-26 13:22:50 +0000 | [diff] [blame^] | 45 | .. class:: PriorityQueue(maxsize=0) |
Raymond Hettinger | 171f391 | 2008-01-16 23:38:16 +0000 | [diff] [blame] | 46 | |
| 47 | Constructor for a priority queue. *maxsize* is an integer that sets the upperbound |
| 48 | limit on the number of items that can be placed in the queue. Insertion will |
| 49 | block once this size has been reached, until queue items are consumed. If |
| 50 | *maxsize* is less than or equal to zero, the queue size is infinite. |
| 51 | |
| 52 | The lowest valued entries are retrieved first (the lowest valued entry is the |
| 53 | one returned by ``sorted(list(entries))[0]``). A typical pattern for entries |
| 54 | is a tuple in the form: ``(priority_number, data)``. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 55 | |
Raymond Hettinger | d59f457 | 2008-01-17 08:07:05 +0000 | [diff] [blame] | 56 | .. versionadded:: 2.6 |
| 57 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 58 | .. exception:: Empty |
| 59 | |
| 60 | Exception raised when non-blocking :meth:`get` (or :meth:`get_nowait`) is called |
| 61 | on a :class:`Queue` object which is empty. |
| 62 | |
| 63 | |
| 64 | .. exception:: Full |
| 65 | |
| 66 | Exception raised when non-blocking :meth:`put` (or :meth:`put_nowait`) is called |
| 67 | on a :class:`Queue` object which is full. |
| 68 | |
Raymond Hettinger | 9324ed8 | 2009-03-09 12:56:23 +0000 | [diff] [blame] | 69 | .. seealso:: |
| 70 | |
| 71 | :class:`collections.deque` is an alternative implementation of unbounded |
| 72 | queues with fast atomic :func:`append` and :func:`popleft` operations that |
| 73 | do not require locking. |
| 74 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 75 | |
| 76 | .. _queueobjects: |
| 77 | |
| 78 | Queue Objects |
| 79 | ------------- |
| 80 | |
Brett Cannon | 89dfbe3 | 2008-02-03 02:34:14 +0000 | [diff] [blame] | 81 | Queue objects (:class:`Queue`, :class:`LifoQueue`, or :class:`PriorityQueue`) |
Georg Brandl | a6168f9 | 2008-05-25 07:20:14 +0000 | [diff] [blame] | 82 | provide the public methods described below. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 83 | |
| 84 | |
| 85 | .. method:: Queue.qsize() |
| 86 | |
Raymond Hettinger | 907cda6 | 2008-01-15 05:46:43 +0000 | [diff] [blame] | 87 | Return the approximate size of the queue. Note, qsize() > 0 doesn't |
| 88 | guarantee that a subsequent get() will not block, nor will qsize() < maxsize |
Skip Montanaro | f233b0c | 2008-01-15 03:40:20 +0000 | [diff] [blame] | 89 | guarantee that put() will not block. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 90 | |
| 91 | |
| 92 | .. method:: Queue.empty() |
| 93 | |
Skip Montanaro | f233b0c | 2008-01-15 03:40:20 +0000 | [diff] [blame] | 94 | Return ``True`` if the queue is empty, ``False`` otherwise. If empty() |
| 95 | returns ``True`` it doesn't guarantee that a subsequent call to put() |
| 96 | will not block. Similarly, if empty() returns ``False`` it doesn't |
| 97 | guarantee that a subsequent call to get() will not block. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 98 | |
| 99 | |
| 100 | .. method:: Queue.full() |
| 101 | |
Skip Montanaro | f233b0c | 2008-01-15 03:40:20 +0000 | [diff] [blame] | 102 | Return ``True`` if the queue is full, ``False`` otherwise. If full() |
| 103 | returns ``True`` it doesn't guarantee that a subsequent call to get() |
| 104 | will not block. Similarly, if full() returns ``False`` it doesn't |
| 105 | guarantee that a subsequent call to put() will not block. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 106 | |
| 107 | |
| 108 | .. method:: Queue.put(item[, block[, timeout]]) |
| 109 | |
| 110 | Put *item* into the queue. If optional args *block* is true and *timeout* is |
| 111 | None (the default), block if necessary until a free slot is available. If |
| 112 | *timeout* is a positive number, it blocks at most *timeout* seconds and raises |
| 113 | the :exc:`Full` exception if no free slot was available within that time. |
| 114 | Otherwise (*block* is false), put an item on the queue if a free slot is |
| 115 | immediately available, else raise the :exc:`Full` exception (*timeout* is |
| 116 | ignored in that case). |
| 117 | |
| 118 | .. versionadded:: 2.3 |
| 119 | The *timeout* parameter. |
| 120 | |
| 121 | |
| 122 | .. method:: Queue.put_nowait(item) |
| 123 | |
| 124 | Equivalent to ``put(item, False)``. |
| 125 | |
| 126 | |
| 127 | .. method:: Queue.get([block[, timeout]]) |
| 128 | |
| 129 | Remove and return an item from the queue. If optional args *block* is true and |
| 130 | *timeout* is None (the default), block if necessary until an item is available. |
| 131 | If *timeout* is a positive number, it blocks at most *timeout* seconds and |
| 132 | raises the :exc:`Empty` exception if no item was available within that time. |
| 133 | Otherwise (*block* is false), return an item if one is immediately available, |
| 134 | else raise the :exc:`Empty` exception (*timeout* is ignored in that case). |
| 135 | |
| 136 | .. versionadded:: 2.3 |
| 137 | The *timeout* parameter. |
| 138 | |
| 139 | |
| 140 | .. method:: Queue.get_nowait() |
| 141 | |
| 142 | Equivalent to ``get(False)``. |
| 143 | |
| 144 | Two methods are offered to support tracking whether enqueued tasks have been |
| 145 | fully processed by daemon consumer threads. |
| 146 | |
| 147 | |
| 148 | .. method:: Queue.task_done() |
| 149 | |
| 150 | Indicate that a formerly enqueued task is complete. Used by queue consumer |
| 151 | threads. For each :meth:`get` used to fetch a task, a subsequent call to |
| 152 | :meth:`task_done` tells the queue that the processing on the task is complete. |
| 153 | |
| 154 | If a :meth:`join` is currently blocking, it will resume when all items have been |
| 155 | processed (meaning that a :meth:`task_done` call was received for every item |
| 156 | that had been :meth:`put` into the queue). |
| 157 | |
| 158 | Raises a :exc:`ValueError` if called more times than there were items placed in |
| 159 | the queue. |
| 160 | |
| 161 | .. versionadded:: 2.5 |
| 162 | |
| 163 | |
| 164 | .. method:: Queue.join() |
| 165 | |
| 166 | Blocks until all items in the queue have been gotten and processed. |
| 167 | |
| 168 | The count of unfinished tasks goes up whenever an item is added to the queue. |
| 169 | The count goes down whenever a consumer thread calls :meth:`task_done` to |
| 170 | indicate that the item was retrieved and all work on it is complete. When the |
Raymond Hettinger | f4ea929 | 2009-03-10 00:06:05 +0000 | [diff] [blame] | 171 | count of unfinished tasks drops to zero, :meth:`join` unblocks. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 172 | |
| 173 | .. versionadded:: 2.5 |
| 174 | |
| 175 | Example of how to wait for enqueued tasks to be completed:: |
| 176 | |
Georg Brandl | a6168f9 | 2008-05-25 07:20:14 +0000 | [diff] [blame] | 177 | def worker(): |
| 178 | while True: |
| 179 | item = q.get() |
| 180 | do_work(item) |
| 181 | q.task_done() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 182 | |
Georg Brandl | a6168f9 | 2008-05-25 07:20:14 +0000 | [diff] [blame] | 183 | q = Queue() |
| 184 | for i in range(num_worker_threads): |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 185 | t = Thread(target=worker) |
Benjamin Peterson | e047d79 | 2009-10-20 03:14:10 +0000 | [diff] [blame] | 186 | t.daemon = True |
Georg Brandl | a6168f9 | 2008-05-25 07:20:14 +0000 | [diff] [blame] | 187 | t.start() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 188 | |
| 189 | for item in source(): |
Georg Brandl | a6168f9 | 2008-05-25 07:20:14 +0000 | [diff] [blame] | 190 | q.put(item) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 191 | |
| 192 | q.join() # block until all tasks are done |
| 193 | |