Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | |
| 2 | :mod:`Queue` --- A synchronized queue class |
| 3 | =========================================== |
| 4 | |
| 5 | .. module:: Queue |
| 6 | :synopsis: A synchronized queue class. |
| 7 | |
| 8 | |
| 9 | The :mod:`Queue` module implements a multi-producer, multi-consumer FIFO queue. |
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 | |
| 16 | The :mod:`Queue` module defines the following class and exception: |
| 17 | |
| 18 | |
| 19 | .. class:: Queue(maxsize) |
| 20 | |
| 21 | Constructor for the class. *maxsize* is an integer that sets the upperbound |
| 22 | limit on the number of items that can be placed in the queue. Insertion will |
| 23 | block once this size has been reached, until queue items are consumed. If |
| 24 | *maxsize* is less than or equal to zero, the queue size is infinite. |
| 25 | |
| 26 | |
| 27 | .. exception:: Empty |
| 28 | |
| 29 | Exception raised when non-blocking :meth:`get` (or :meth:`get_nowait`) is called |
| 30 | on a :class:`Queue` object which is empty. |
| 31 | |
| 32 | |
| 33 | .. exception:: Full |
| 34 | |
| 35 | Exception raised when non-blocking :meth:`put` (or :meth:`put_nowait`) is called |
| 36 | on a :class:`Queue` object which is full. |
| 37 | |
| 38 | |
| 39 | .. _queueobjects: |
| 40 | |
| 41 | Queue Objects |
| 42 | ------------- |
| 43 | |
| 44 | Class :class:`Queue` implements queue objects and has the methods described |
| 45 | below. This class can be derived from in order to implement other queue |
| 46 | organizations (e.g. stack) but the inheritable interface is not described here. |
| 47 | See the source code for details. The public methods are: |
| 48 | |
| 49 | |
| 50 | .. method:: Queue.qsize() |
| 51 | |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 52 | Return the approximate size of the queue. Note, qsize() > 0 doesn't |
| 53 | guarantee that a subsequent get() will not block, nor will qsize() < maxsize |
| 54 | guarantee that put() will not block. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 55 | |
| 56 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 57 | .. method:: Queue.put(item[, block[, timeout]]) |
| 58 | |
| 59 | Put *item* into the queue. If optional args *block* is true and *timeout* is |
| 60 | None (the default), block if necessary until a free slot is available. If |
| 61 | *timeout* is a positive number, it blocks at most *timeout* seconds and raises |
| 62 | the :exc:`Full` exception if no free slot was available within that time. |
| 63 | Otherwise (*block* is false), put an item on the queue if a free slot is |
| 64 | immediately available, else raise the :exc:`Full` exception (*timeout* is |
| 65 | ignored in that case). |
| 66 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 67 | |
| 68 | .. method:: Queue.put_nowait(item) |
| 69 | |
| 70 | Equivalent to ``put(item, False)``. |
| 71 | |
| 72 | |
| 73 | .. method:: Queue.get([block[, timeout]]) |
| 74 | |
| 75 | Remove and return an item from the queue. If optional args *block* is true and |
| 76 | *timeout* is None (the default), block if necessary until an item is available. |
| 77 | If *timeout* is a positive number, it blocks at most *timeout* seconds and |
| 78 | raises the :exc:`Empty` exception if no item was available within that time. |
| 79 | Otherwise (*block* is false), return an item if one is immediately available, |
| 80 | else raise the :exc:`Empty` exception (*timeout* is ignored in that case). |
| 81 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 82 | |
| 83 | .. method:: Queue.get_nowait() |
| 84 | |
| 85 | Equivalent to ``get(False)``. |
| 86 | |
| 87 | Two methods are offered to support tracking whether enqueued tasks have been |
| 88 | fully processed by daemon consumer threads. |
| 89 | |
| 90 | |
| 91 | .. method:: Queue.task_done() |
| 92 | |
| 93 | Indicate that a formerly enqueued task is complete. Used by queue consumer |
| 94 | threads. For each :meth:`get` used to fetch a task, a subsequent call to |
| 95 | :meth:`task_done` tells the queue that the processing on the task is complete. |
| 96 | |
| 97 | If a :meth:`join` is currently blocking, it will resume when all items have been |
| 98 | processed (meaning that a :meth:`task_done` call was received for every item |
| 99 | that had been :meth:`put` into the queue). |
| 100 | |
| 101 | Raises a :exc:`ValueError` if called more times than there were items placed in |
| 102 | the queue. |
| 103 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 104 | |
| 105 | .. method:: Queue.join() |
| 106 | |
| 107 | Blocks until all items in the queue have been gotten and processed. |
| 108 | |
| 109 | The count of unfinished tasks goes up whenever an item is added to the queue. |
| 110 | The count goes down whenever a consumer thread calls :meth:`task_done` to |
| 111 | indicate that the item was retrieved and all work on it is complete. When the |
| 112 | count of unfinished tasks drops to zero, join() unblocks. |
| 113 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 114 | |
| 115 | Example of how to wait for enqueued tasks to be completed:: |
| 116 | |
| 117 | def worker(): |
| 118 | while True: |
| 119 | item = q.get() |
| 120 | do_work(item) |
| 121 | q.task_done() |
| 122 | |
| 123 | q = Queue() |
| 124 | for i in range(num_worker_threads): |
| 125 | t = Thread(target=worker) |
| 126 | t.setDaemon(True) |
| 127 | t.start() |
| 128 | |
| 129 | for item in source(): |
| 130 | q.put(item) |
| 131 | |
| 132 | q.join() # block until all tasks are done |
| 133 | |