blob: 0ec5900bef5bbff6da218e066b9d4f1ac8998876 [file] [log] [blame]
Alexandre Vassalottif260e442008-05-11 19:59:59 +00001:mod:`queue` --- A synchronized queue class
Georg Brandl116aa622007-08-15 14:28:22 +00002===========================================
3
Alexandre Vassalottif260e442008-05-11 19:59:59 +00004.. module:: queue
Georg Brandl116aa622007-08-15 14:28:22 +00005 :synopsis: A synchronized queue class.
6
Raymond Hettinger10480942011-01-10 03:26:08 +00007**Source code:** :source:`Lib/queue.py`
Georg Brandl116aa622007-08-15 14:28:22 +00008
Raymond Hettinger4f707fd2011-01-10 19:54:11 +00009--------------
10
Alexandre Vassalottif260e442008-05-11 19:59:59 +000011The :mod:`queue` module implements multi-producer, multi-consumer queues.
Thomas Wouters89d996e2007-09-08 17:39:28 +000012It is especially useful in threaded programming when information must be
Georg Brandl116aa622007-08-15 14:28:22 +000013exchanged safely between multiple threads. The :class:`Queue` class in this
Zackery Spytzeef05962018-09-29 10:07:11 -060014module implements all the required locking semantics.
Georg Brandl116aa622007-08-15 14:28:22 +000015
R David Murrayb98b37f2012-05-08 21:28:24 -040016The module implements three types of queue, which differ only in the order in
Serhiy Storchaka4ecfa452016-05-16 09:31:54 +030017which the entries are retrieved. In a :abbr:`FIFO (first-in, first-out)`
18queue, the first tasks added are the first retrieved. In a
19:abbr:`LIFO (last-in, first-out)` queue, the most recently added entry is
Raymond Hettinger35641462008-01-17 00:13:27 +000020the first retrieved (operating like a stack). With a priority queue,
21the entries are kept sorted (using the :mod:`heapq` module) and the
22lowest valued entry is retrieved first.
Georg Brandl116aa622007-08-15 14:28:22 +000023
Antoine Pitrou94e16962018-01-16 00:27:16 +010024Internally, those three types of queues use locks to temporarily block
25competing threads; however, they are not designed to handle reentrancy
26within a thread.
27
28In addition, the module implements a "simple"
Julien Palardacef6902018-10-20 00:27:49 +020029:abbr:`FIFO (first-in, first-out)` queue type, :class:`SimpleQueue`, whose
30specific implementation provides additional guarantees
Antoine Pitrou94e16962018-01-16 00:27:16 +010031in exchange for the smaller functionality.
Éric Araujo6e6cb8e2010-11-16 19:13:50 +000032
Alexandre Vassalottif260e442008-05-11 19:59:59 +000033The :mod:`queue` module defines the following classes and exceptions:
Georg Brandl116aa622007-08-15 14:28:22 +000034
Andrew M. Kuchling2b600e52010-02-26 13:35:56 +000035.. class:: Queue(maxsize=0)
Georg Brandl116aa622007-08-15 14:28:22 +000036
Serhiy Storchaka4ecfa452016-05-16 09:31:54 +030037 Constructor for a :abbr:`FIFO (first-in, first-out)` queue. *maxsize* is
38 an integer that sets the upperbound
Georg Brandl116aa622007-08-15 14:28:22 +000039 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
Andrew M. Kuchling2b600e52010-02-26 13:35:56 +000043.. class:: LifoQueue(maxsize=0)
Raymond Hettinger35641462008-01-17 00:13:27 +000044
Serhiy Storchaka4ecfa452016-05-16 09:31:54 +030045 Constructor for a :abbr:`LIFO (last-in, first-out)` queue. *maxsize* is
46 an integer that sets the upperbound
Raymond Hettinger35641462008-01-17 00:13:27 +000047 limit on the number of items that can be placed in the queue. Insertion will
48 block once this size has been reached, until queue items are consumed. If
49 *maxsize* is less than or equal to zero, the queue size is infinite.
50
Christian Heimes679db4a2008-01-18 09:56:22 +000051
Andrew M. Kuchling2b600e52010-02-26 13:35:56 +000052.. class:: PriorityQueue(maxsize=0)
Raymond Hettinger35641462008-01-17 00:13:27 +000053
54 Constructor for a priority queue. *maxsize* is an integer that sets the upperbound
55 limit on the number of items that can be placed in the queue. Insertion will
56 block once this size has been reached, until queue items are consumed. If
57 *maxsize* is less than or equal to zero, the queue size is infinite.
58
59 The lowest valued entries are retrieved first (the lowest valued entry is the
60 one returned by ``sorted(list(entries))[0]``). A typical pattern for entries
61 is a tuple in the form: ``(priority_number, data)``.
Georg Brandl116aa622007-08-15 14:28:22 +000062
Raymond Hettinger0c3be962018-01-11 22:06:34 -080063 If the *data* elements are not comparable, the data can be wrapped in a class
64 that ignores the data item and only compares the priority number::
65
66 from dataclasses import dataclass, field
67 from typing import Any
68
69 @dataclass(order=True)
70 class PrioritizedItem:
71 priority: int
72 item: Any=field(compare=False)
Christian Heimes679db4a2008-01-18 09:56:22 +000073
Antoine Pitrou94e16962018-01-16 00:27:16 +010074.. class:: SimpleQueue()
75
76 Constructor for an unbounded :abbr:`FIFO (first-in, first-out)` queue.
77 Simple queues lack advanced functionality such as task tracking.
78
79 .. versionadded:: 3.7
80
81
Georg Brandl116aa622007-08-15 14:28:22 +000082.. exception:: Empty
83
Serhiy Storchaka9e0ae532013-08-24 00:23:38 +030084 Exception raised when non-blocking :meth:`~Queue.get` (or
85 :meth:`~Queue.get_nowait`) is called
Georg Brandl116aa622007-08-15 14:28:22 +000086 on a :class:`Queue` object which is empty.
87
88
89.. exception:: Full
90
Serhiy Storchaka9e0ae532013-08-24 00:23:38 +030091 Exception raised when non-blocking :meth:`~Queue.put` (or
92 :meth:`~Queue.put_nowait`) is called
Georg Brandl116aa622007-08-15 14:28:22 +000093 on a :class:`Queue` object which is full.
94
95
96.. _queueobjects:
97
98Queue Objects
99-------------
100
Christian Heimes292d3512008-02-03 16:51:08 +0000101Queue objects (:class:`Queue`, :class:`LifoQueue`, or :class:`PriorityQueue`)
Georg Brandl48310cd2009-01-03 21:18:54 +0000102provide the public methods described below.
Georg Brandl116aa622007-08-15 14:28:22 +0000103
104
105.. method:: Queue.qsize()
106
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000107 Return the approximate size of the queue. Note, qsize() > 0 doesn't
108 guarantee that a subsequent get() will not block, nor will qsize() < maxsize
109 guarantee that put() will not block.
Georg Brandl116aa622007-08-15 14:28:22 +0000110
111
Raymond Hettinger47aa9892009-03-07 14:07:37 +0000112.. method:: Queue.empty()
113
114 Return ``True`` if the queue is empty, ``False`` otherwise. If empty()
115 returns ``True`` it doesn't guarantee that a subsequent call to put()
116 will not block. Similarly, if empty() returns ``False`` it doesn't
117 guarantee that a subsequent call to get() will not block.
118
119
120.. method:: Queue.full()
121
122 Return ``True`` if the queue is full, ``False`` otherwise. If full()
123 returns ``True`` it doesn't guarantee that a subsequent call to get()
124 will not block. Similarly, if full() returns ``False`` it doesn't
125 guarantee that a subsequent call to put() will not block.
126
127
Georg Brandl18244152009-09-02 20:34:52 +0000128.. method:: Queue.put(item, block=True, timeout=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000129
130 Put *item* into the queue. If optional args *block* is true and *timeout* is
Serhiy Storchakaecf41da2016-10-19 16:29:26 +0300131 ``None`` (the default), block if necessary until a free slot is available. If
Georg Brandl116aa622007-08-15 14:28:22 +0000132 *timeout* is a positive number, it blocks at most *timeout* seconds and raises
133 the :exc:`Full` exception if no free slot was available within that time.
134 Otherwise (*block* is false), put an item on the queue if a free slot is
135 immediately available, else raise the :exc:`Full` exception (*timeout* is
136 ignored in that case).
137
Georg Brandl116aa622007-08-15 14:28:22 +0000138
139.. method:: Queue.put_nowait(item)
140
141 Equivalent to ``put(item, False)``.
142
143
Georg Brandl18244152009-09-02 20:34:52 +0000144.. method:: Queue.get(block=True, timeout=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000145
146 Remove and return an item from the queue. If optional args *block* is true and
Serhiy Storchakaecf41da2016-10-19 16:29:26 +0300147 *timeout* is ``None`` (the default), block if necessary until an item is available.
Georg Brandl116aa622007-08-15 14:28:22 +0000148 If *timeout* is a positive number, it blocks at most *timeout* seconds and
149 raises the :exc:`Empty` exception if no item was available within that time.
150 Otherwise (*block* is false), return an item if one is immediately available,
151 else raise the :exc:`Empty` exception (*timeout* is ignored in that case).
152
Stephen Rosen713a8ae2019-03-25 15:55:20 -0400153 Prior to 3.0 on POSIX systems, and for all versions on Windows, if
154 *block* is true and *timeout* is ``None``, this operation goes into
155 an uninterruptible wait on an underlying lock. This means that no exceptions
156 can occur, and in particular a SIGINT will not trigger a :exc:`KeyboardInterrupt`.
157
Georg Brandl116aa622007-08-15 14:28:22 +0000158
159.. method:: Queue.get_nowait()
160
161 Equivalent to ``get(False)``.
162
163Two methods are offered to support tracking whether enqueued tasks have been
164fully processed by daemon consumer threads.
165
166
167.. method:: Queue.task_done()
168
169 Indicate that a formerly enqueued task is complete. Used by queue consumer
170 threads. For each :meth:`get` used to fetch a task, a subsequent call to
171 :meth:`task_done` tells the queue that the processing on the task is complete.
172
173 If a :meth:`join` is currently blocking, it will resume when all items have been
174 processed (meaning that a :meth:`task_done` call was received for every item
175 that had been :meth:`put` into the queue).
176
177 Raises a :exc:`ValueError` if called more times than there were items placed in
178 the queue.
179
Georg Brandl116aa622007-08-15 14:28:22 +0000180
181.. method:: Queue.join()
182
183 Blocks until all items in the queue have been gotten and processed.
184
185 The count of unfinished tasks goes up whenever an item is added to the queue.
186 The count goes down whenever a consumer thread calls :meth:`task_done` to
187 indicate that the item was retrieved and all work on it is complete. When the
Raymond Hettinger28c013d2009-03-10 00:07:25 +0000188 count of unfinished tasks drops to zero, :meth:`join` unblocks.
Georg Brandl116aa622007-08-15 14:28:22 +0000189
Georg Brandl116aa622007-08-15 14:28:22 +0000190
191Example of how to wait for enqueued tasks to be completed::
192
Raymond Hettinger88499f12020-04-26 18:11:27 -0700193 import threading, queue
194
195 q = queue.Queue()
196
Victor Stinnerde311342015-03-18 14:05:43 +0100197 def worker():
198 while True:
199 item = q.get()
Raymond Hettinger88499f12020-04-26 18:11:27 -0700200 print(f'Working on {item}')
201 print(f'Finished {item}')
Victor Stinnerde311342015-03-18 14:05:43 +0100202 q.task_done()
Georg Brandl116aa622007-08-15 14:28:22 +0000203
Raymond Hettinger88499f12020-04-26 18:11:27 -0700204 # turn-on the worker thread
205 threading.Thread(target=worker, daemon=True).start()
Georg Brandl116aa622007-08-15 14:28:22 +0000206
Raymond Hettinger88499f12020-04-26 18:11:27 -0700207 # send thirty task requests to the worker
208 for item in range(30):
Victor Stinnerde311342015-03-18 14:05:43 +0100209 q.put(item)
Raymond Hettinger88499f12020-04-26 18:11:27 -0700210 print('All task requests sent\n', end='')
Georg Brandl116aa622007-08-15 14:28:22 +0000211
Victor Stinnerde311342015-03-18 14:05:43 +0100212 # block until all tasks are done
213 q.join()
Raymond Hettinger88499f12020-04-26 18:11:27 -0700214 print('All work completed')
Georg Brandl116aa622007-08-15 14:28:22 +0000215
Antoine Pitrou696efdd2011-01-07 19:16:12 +0000216
Antoine Pitrou94e16962018-01-16 00:27:16 +0100217SimpleQueue Objects
218-------------------
219
220:class:`SimpleQueue` objects provide the public methods described below.
221
222.. method:: SimpleQueue.qsize()
223
224 Return the approximate size of the queue. Note, qsize() > 0 doesn't
225 guarantee that a subsequent get() will not block.
226
227
228.. method:: SimpleQueue.empty()
229
230 Return ``True`` if the queue is empty, ``False`` otherwise. If empty()
231 returns ``False`` it doesn't guarantee that a subsequent call to get()
232 will not block.
233
234
235.. method:: SimpleQueue.put(item, block=True, timeout=None)
236
237 Put *item* into the queue. The method never blocks and always succeeds
238 (except for potential low-level errors such as failure to allocate memory).
239 The optional args *block* and *timeout* are ignored and only provided
240 for compatibility with :meth:`Queue.put`.
241
242 .. impl-detail::
243 This method has a C implementation which is reentrant. That is, a
244 ``put()`` or ``get()`` call can be interrupted by another ``put()``
245 call in the same thread without deadlocking or corrupting internal
246 state inside the queue. This makes it appropriate for use in
247 destructors such as ``__del__`` methods or :mod:`weakref` callbacks.
248
249
250.. method:: SimpleQueue.put_nowait(item)
251
252 Equivalent to ``put(item)``, provided for compatibility with
253 :meth:`Queue.put_nowait`.
254
255
256.. method:: SimpleQueue.get(block=True, timeout=None)
257
258 Remove and return an item from the queue. If optional args *block* is true and
259 *timeout* is ``None`` (the default), block if necessary until an item is available.
260 If *timeout* is a positive number, it blocks at most *timeout* seconds and
261 raises the :exc:`Empty` exception if no item was available within that time.
262 Otherwise (*block* is false), return an item if one is immediately available,
263 else raise the :exc:`Empty` exception (*timeout* is ignored in that case).
264
265
266.. method:: SimpleQueue.get_nowait()
267
268 Equivalent to ``get(False)``.
269
270
Antoine Pitrou696efdd2011-01-07 19:16:12 +0000271.. seealso::
272
273 Class :class:`multiprocessing.Queue`
274 A queue class for use in a multi-processing (rather than multi-threading)
275 context.
276
Georg Brandl2f2a9f72011-01-07 20:58:25 +0000277 :class:`collections.deque` is an alternative implementation of unbounded
Serhiy Storchaka9e0ae532013-08-24 00:23:38 +0300278 queues with fast atomic :meth:`~collections.deque.append` and
Windson yang98b85352018-11-05 06:34:22 +0800279 :meth:`~collections.deque.popleft` operations that do not require locking
280 and also support indexing.