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