blob: aafd717ce4b9abbf2703a0a768ccf5a8c9091d8a [file] [log] [blame]
Georg Brandl7a148c22008-05-12 10:03:16 +00001:mod:`queue` --- A synchronized queue class
Georg Brandl8ec7f652007-08-15 14:28:01 +00002===========================================
3
4.. module:: Queue
Georg Brandl7a148c22008-05-12 10:03:16 +00005 :synopsis: Old name for the queue module.
6
7.. module:: queue
Georg Brandl8ec7f652007-08-15 14:28:01 +00008 :synopsis: A synchronized queue class.
9
Alexandre Vassalotti73812bf2008-05-11 20:04:03 +000010.. note::
Georg Brandl7a148c22008-05-12 10:03:16 +000011 The :mod:`Queue` module has been renamed to :mod:`queue` in Python 3.0. It
12 is importable under both names in Python 2.6 and the rest of the 2.x series.
Georg Brandl8ec7f652007-08-15 14:28:01 +000013
Georg Brandl7a148c22008-05-12 10:03:16 +000014
15The :mod:`queue` module implements multi-producer, multi-consumer queues.
Mark Summerfieldfcb444a2007-09-04 08:16:15 +000016It is especially useful in threaded programming when information must be
Georg Brandl8ec7f652007-08-15 14:28:01 +000017exchanged safely between multiple threads. The :class:`Queue` class in this
18module implements all the required locking semantics. It depends on the
Mark Summerfieldfcb444a2007-09-04 08:16:15 +000019availability of thread support in Python; see the :mod:`threading`
20module.
Georg Brandl8ec7f652007-08-15 14:28:01 +000021
Raymond Hettinger171f3912008-01-16 23:38:16 +000022Implements three types of queue whose only difference is the order that
23the entries are retrieved. In a FIFO queue, the first tasks added are
24the first retrieved. In a LIFO queue, the most recently added entry is
25the first retrieved (operating like a stack). With a priority queue,
26the entries are kept sorted (using the :mod:`heapq` module) and the
27lowest valued entry is retrieved first.
Georg Brandl8ec7f652007-08-15 14:28:01 +000028
Georg Brandl7a148c22008-05-12 10:03:16 +000029The :mod:`queue` module defines the following classes and exceptions:
Georg Brandl8ec7f652007-08-15 14:28:01 +000030
31.. class:: Queue(maxsize)
32
Raymond Hettinger171f3912008-01-16 23:38:16 +000033 Constructor for a FIFO queue. *maxsize* is an integer that sets the upperbound
Georg Brandl8ec7f652007-08-15 14:28:01 +000034 limit on the number of items that can be placed in the queue. Insertion will
35 block once this size has been reached, until queue items are consumed. If
36 *maxsize* is less than or equal to zero, the queue size is infinite.
37
Raymond Hettinger171f3912008-01-16 23:38:16 +000038.. class:: LifoQueue(maxsize)
39
40 Constructor for a LIFO queue. *maxsize* is an integer that sets the upperbound
41 limit on the number of items that can be placed in the queue. Insertion will
42 block once this size has been reached, until queue items are consumed. If
43 *maxsize* is less than or equal to zero, the queue size is infinite.
44
Raymond Hettingerd59f4572008-01-17 08:07:05 +000045 .. versionadded:: 2.6
46
Raymond Hettinger171f3912008-01-16 23:38:16 +000047.. class:: PriorityQueue(maxsize)
48
49 Constructor for a priority queue. *maxsize* is an integer that sets the upperbound
50 limit on the number of items that can be placed in the queue. Insertion will
51 block once this size has been reached, until queue items are consumed. If
52 *maxsize* is less than or equal to zero, the queue size is infinite.
53
54 The lowest valued entries are retrieved first (the lowest valued entry is the
55 one returned by ``sorted(list(entries))[0]``). A typical pattern for entries
56 is a tuple in the form: ``(priority_number, data)``.
Georg Brandl8ec7f652007-08-15 14:28:01 +000057
Raymond Hettingerd59f4572008-01-17 08:07:05 +000058 .. versionadded:: 2.6
59
Georg Brandl8ec7f652007-08-15 14:28:01 +000060.. exception:: Empty
61
62 Exception raised when non-blocking :meth:`get` (or :meth:`get_nowait`) is called
63 on a :class:`Queue` object which is empty.
64
65
66.. exception:: Full
67
68 Exception raised when non-blocking :meth:`put` (or :meth:`put_nowait`) is called
69 on a :class:`Queue` object which is full.
70
71
72.. _queueobjects:
73
74Queue Objects
75-------------
76
Brett Cannon89dfbe32008-02-03 02:34:14 +000077Queue objects (:class:`Queue`, :class:`LifoQueue`, or :class:`PriorityQueue`)
Raymond Hettinger171f3912008-01-16 23:38:16 +000078provide the public methods described below.
Georg Brandl8ec7f652007-08-15 14:28:01 +000079
80
81.. method:: Queue.qsize()
82
Raymond Hettinger907cda62008-01-15 05:46:43 +000083 Return the approximate size of the queue. Note, qsize() > 0 doesn't
84 guarantee that a subsequent get() will not block, nor will qsize() < maxsize
Skip Montanarof233b0c2008-01-15 03:40:20 +000085 guarantee that put() will not block.
Georg Brandl8ec7f652007-08-15 14:28:01 +000086
87
88.. method:: Queue.empty()
89
Skip Montanarof233b0c2008-01-15 03:40:20 +000090 Return ``True`` if the queue is empty, ``False`` otherwise. If empty()
91 returns ``True`` it doesn't guarantee that a subsequent call to put()
92 will not block. Similarly, if empty() returns ``False`` it doesn't
93 guarantee that a subsequent call to get() will not block.
Georg Brandl8ec7f652007-08-15 14:28:01 +000094
95
96.. method:: Queue.full()
97
Skip Montanarof233b0c2008-01-15 03:40:20 +000098 Return ``True`` if the queue is full, ``False`` otherwise. If full()
99 returns ``True`` it doesn't guarantee that a subsequent call to get()
100 will not block. Similarly, if full() returns ``False`` it doesn't
101 guarantee that a subsequent call to put() will not block.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000102
103
104.. method:: Queue.put(item[, block[, timeout]])
105
106 Put *item* into the queue. If optional args *block* is true and *timeout* is
107 None (the default), block if necessary until a free slot is available. If
108 *timeout* is a positive number, it blocks at most *timeout* seconds and raises
109 the :exc:`Full` exception if no free slot was available within that time.
110 Otherwise (*block* is false), put an item on the queue if a free slot is
111 immediately available, else raise the :exc:`Full` exception (*timeout* is
112 ignored in that case).
113
114 .. versionadded:: 2.3
115 The *timeout* parameter.
116
117
118.. method:: Queue.put_nowait(item)
119
120 Equivalent to ``put(item, False)``.
121
122
123.. method:: Queue.get([block[, timeout]])
124
125 Remove and return an item from the queue. If optional args *block* is true and
126 *timeout* is None (the default), block if necessary until an item is available.
127 If *timeout* is a positive number, it blocks at most *timeout* seconds and
128 raises the :exc:`Empty` exception if no item was available within that time.
129 Otherwise (*block* is false), return an item if one is immediately available,
130 else raise the :exc:`Empty` exception (*timeout* is ignored in that case).
131
132 .. versionadded:: 2.3
133 The *timeout* parameter.
134
135
136.. method:: Queue.get_nowait()
137
138 Equivalent to ``get(False)``.
139
140Two methods are offered to support tracking whether enqueued tasks have been
141fully processed by daemon consumer threads.
142
143
144.. method:: Queue.task_done()
145
146 Indicate that a formerly enqueued task is complete. Used by queue consumer
147 threads. For each :meth:`get` used to fetch a task, a subsequent call to
148 :meth:`task_done` tells the queue that the processing on the task is complete.
149
150 If a :meth:`join` is currently blocking, it will resume when all items have been
151 processed (meaning that a :meth:`task_done` call was received for every item
152 that had been :meth:`put` into the queue).
153
154 Raises a :exc:`ValueError` if called more times than there were items placed in
155 the queue.
156
157 .. versionadded:: 2.5
158
159
160.. method:: Queue.join()
161
162 Blocks until all items in the queue have been gotten and processed.
163
164 The count of unfinished tasks goes up whenever an item is added to the queue.
165 The count goes down whenever a consumer thread calls :meth:`task_done` to
166 indicate that the item was retrieved and all work on it is complete. When the
167 count of unfinished tasks drops to zero, join() unblocks.
168
169 .. versionadded:: 2.5
170
171Example of how to wait for enqueued tasks to be completed::
172
173 def worker():
174 while True:
175 item = q.get()
176 do_work(item)
177 q.task_done()
178
179 q = Queue()
180 for i in range(num_worker_threads):
181 t = Thread(target=worker)
182 t.setDaemon(True)
183 t.start()
184
185 for item in source():
186 q.put(item)
187
188 q.join() # block until all tasks are done
189