@@ -93,6 +93,14 @@ The :mod:`queue` module defines the following classes and exceptions:
9393 on a :class: `Queue ` object which is full.
9494
9595
96+ .. exception :: ShutDown
97+
98+ Exception raised when :meth: `~Queue.put ` or :meth: `~Queue.get ` is called on
99+ a :class: `Queue ` object which has been shut down.
100+
101+ .. versionadded :: 3.13
102+
103+
96104.. _queueobjects :
97105
98106Queue Objects
@@ -135,6 +143,8 @@ provide the public methods described below.
135143 immediately available, else raise the :exc: `Full ` exception (*timeout * is
136144 ignored in that case).
137145
146+ Raises :exc: `ShutDown ` if the queue has been shut down.
147+
138148
139149.. method :: Queue.put_nowait(item)
140150
@@ -155,6 +165,9 @@ provide the public methods described below.
155165 an uninterruptible wait on an underlying lock. This means that no exceptions
156166 can occur, and in particular a SIGINT will not trigger a :exc: `KeyboardInterrupt `.
157167
168+ Raises :exc: `ShutDown ` if the queue has been shut down and is empty, or if
169+ the queue has been shut down immediately.
170+
158171
159172.. method :: Queue.get_nowait()
160173
@@ -177,6 +190,8 @@ fully processed by daemon consumer threads.
177190 Raises a :exc: `ValueError ` if called more times than there were items placed in
178191 the queue.
179192
193+ Raises :exc: `ShutDown ` if the queue has been shut down immediately.
194+
180195
181196.. method :: Queue.join()
182197
@@ -187,6 +202,8 @@ fully processed by daemon consumer threads.
187202 indicate that the item was retrieved and all work on it is complete. When the
188203 count of unfinished tasks drops to zero, :meth: `join ` unblocks.
189204
205+ Raises :exc: `ShutDown ` if the queue has been shut down immediately.
206+
190207
191208Example of how to wait for enqueued tasks to be completed::
192209
@@ -214,6 +231,27 @@ Example of how to wait for enqueued tasks to be completed::
214231 print('All work completed')
215232
216233
234+ Terminating queues
235+ ^^^^^^^^^^^^^^^^^^
236+
237+ :class: `Queue ` objects can be made to prevent further interaction by shutting
238+ them down.
239+
240+ .. method :: Queue.shutdown(immediate=False)
241+
242+ Shut down the queue, making :meth: `~Queue.get ` and :meth: `~Queue.put ` raise
243+ :exc: `ShutDown `.
244+
245+ By default, :meth: `~Queue.get ` on a shut down queue will only raise once the
246+ queue is empty. Set *immediate * to true to make :meth: `~Queue.get ` raise
247+ immediately instead.
248+
249+ All blocked callers of :meth: `~Queue.put ` will be unblocked. If *immediate *
250+ is true, also unblock callers of :meth: `~Queue.get ` and :meth: `~Queue.join `.
251+
252+ .. versionadded :: 3.13
253+
254+
217255SimpleQueue Objects
218256-------------------
219257
0 commit comments