Skip to content

Commit d9feec4

Browse files
[3.13] Fix docs for Queue.shutdown (gh-137028) (gh-137081)
1 parent 74503ac commit d9feec4

File tree

4 files changed

+58
-35
lines changed

4 files changed

+58
-35
lines changed

Doc/library/asyncio-queue.rst

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,33 @@ Queue
102102

103103
.. method:: shutdown(immediate=False)
104104

105-
Shut down the queue, making :meth:`~Queue.get` and :meth:`~Queue.put`
105+
Put a :class:`Queue` instance into a shutdown mode.
106+
107+
The queue can no longer grow.
108+
Future calls to :meth:`~Queue.put` raise :exc:`QueueShutDown`.
109+
Currently blocked callers of :meth:`~Queue.put` will be unblocked
110+
and will raise :exc:`QueueShutDown` in the formerly blocked thread.
111+
112+
If *immediate* is false (the default), the queue can be wound
113+
down normally with :meth:`~Queue.get` calls to extract tasks
114+
that have already been loaded.
115+
116+
And if :meth:`~Queue.task_done` is called for each remaining task, a
117+
pending :meth:`~Queue.join` will be unblocked normally.
118+
119+
Once the queue is empty, future calls to :meth:`~Queue.get` will
106120
raise :exc:`QueueShutDown`.
107121

108-
By default, :meth:`~Queue.get` on a shut down queue will only
109-
raise once the queue is empty. Set *immediate* to true to make
110-
:meth:`~Queue.get` raise immediately instead.
122+
If *immediate* is true, the queue is terminated immediately.
123+
The queue is drained to be completely empty. All callers of
124+
:meth:`~Queue.join` are unblocked regardless of the number
125+
of unfinished tasks. Blocked callers of :meth:`~Queue.get`
126+
are unblocked and will raise :exc:`QueueShutDown` because the
127+
queue is empty.
111128

112-
All blocked callers of :meth:`~Queue.put` and :meth:`~Queue.get`
113-
will be unblocked. If *immediate* is true, a task will be marked
114-
as done for each remaining item in the queue, which may unblock
115-
callers of :meth:`~Queue.join`.
129+
Use caution when using :meth:`~Queue.join` with *immediate* set
130+
to true. This unblocks the join even when no work has been done
131+
on the tasks, violating the usual invariant for joining a queue.
116132

117133
.. versionadded:: 3.13
118134

@@ -129,9 +145,6 @@ Queue
129145
call was received for every item that had been :meth:`~Queue.put`
130146
into the queue).
131147

132-
``shutdown(immediate=True)`` calls :meth:`task_done` for each
133-
remaining item in the queue.
134-
135148
Raises :exc:`ValueError` if called more times than there were
136149
items placed in the queue.
137150

Doc/library/queue.rst

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,6 @@ fully processed by daemon consumer threads.
187187
processed (meaning that a :meth:`task_done` call was received for every item
188188
that had been :meth:`put` into the queue).
189189

190-
``shutdown(immediate=True)`` calls :meth:`task_done` for each remaining item
191-
in the queue.
192-
193190
Raises a :exc:`ValueError` if called more times than there were items placed in
194191
the queue.
195192

@@ -204,6 +201,9 @@ fully processed by daemon consumer threads.
204201
count of unfinished tasks drops to zero, :meth:`join` unblocks.
205202

206203

204+
Waiting for task completion
205+
^^^^^^^^^^^^^^^^^^^^^^^^^^^
206+
207207
Example of how to wait for enqueued tasks to be completed::
208208

209209
import threading
@@ -233,22 +233,38 @@ Example of how to wait for enqueued tasks to be completed::
233233
Terminating queues
234234
^^^^^^^^^^^^^^^^^^
235235

236-
:class:`Queue` objects can be made to prevent further interaction by shutting
237-
them down.
236+
When no longer needed, :class:`Queue` objects can be wound down
237+
until empty or terminated immediately with a hard shutdown.
238238

239239
.. method:: Queue.shutdown(immediate=False)
240240

241-
Shut down the queue, making :meth:`~Queue.get` and :meth:`~Queue.put` raise
242-
:exc:`ShutDown`.
241+
Put a :class:`Queue` instance into a shutdown mode.
242+
243+
The queue can no longer grow.
244+
Future calls to :meth:`~Queue.put` raise :exc:`ShutDown`.
245+
Currently blocked callers of :meth:`~Queue.put` will be unblocked
246+
and will raise :exc:`ShutDown` in the formerly blocked thread.
247+
248+
If *immediate* is false (the default), the queue can be wound
249+
down normally with :meth:`~Queue.get` calls to extract tasks
250+
that have already been loaded.
251+
252+
And if :meth:`~Queue.task_done` is called for each remaining task, a
253+
pending :meth:`~Queue.join` will be unblocked normally.
254+
255+
Once the queue is empty, future calls to :meth:`~Queue.get` will
256+
raise :exc:`ShutDown`.
243257

244-
By default, :meth:`~Queue.get` on a shut down queue will only raise once the
245-
queue is empty. Set *immediate* to true to make :meth:`~Queue.get` raise
246-
immediately instead.
258+
If *immediate* is true, the queue is terminated immediately.
259+
The queue is drained to be completely empty. All callers of
260+
:meth:`~Queue.join` are unblocked regardless of the number
261+
of unfinished tasks. Blocked callers of :meth:`~Queue.get`
262+
are unblocked and will raise :exc:`ShutDown` because the
263+
queue is empty.
247264

248-
All blocked callers of :meth:`~Queue.put` and :meth:`~Queue.get` will be
249-
unblocked. If *immediate* is true, a task will be marked as done for each
250-
remaining item in the queue, which may unblock callers of
251-
:meth:`~Queue.join`.
265+
Use caution when using :meth:`~Queue.join` with *immediate* set
266+
to true. This unblocks the join even when no work has been done
267+
on the tasks, violating the usual invariant for joining a queue.
252268

253269
.. versionadded:: 3.13
254270

Lib/asyncio/queues.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,6 @@ def task_done(self):
227227
been processed (meaning that a task_done() call was received for every
228228
item that had been put() into the queue).
229229
230-
shutdown(immediate=True) calls task_done() for each remaining item in
231-
the queue.
232-
233230
Raises ValueError if called more times than there were items placed in
234231
the queue.
235232
"""
@@ -257,8 +254,8 @@ def shutdown(self, immediate=False):
257254
'immediate' to True to make gets raise immediately instead.
258255
259256
All blocked callers of put() and get() will be unblocked. If
260-
'immediate', a task is marked as done for each item remaining in
261-
the queue, which may unblock callers of join().
257+
'immediate', unblock callers of join() regardless of the
258+
number of unfinished tasks.
262259
"""
263260
self._is_shutdown = True
264261
if immediate:

Lib/queue.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,6 @@ def task_done(self):
8080
have been processed (meaning that a task_done() call was received
8181
for every item that had been put() into the queue).
8282
83-
shutdown(immediate=True) calls task_done() for each remaining item in
84-
the queue.
85-
8683
Raises a ValueError if called more times than there were items
8784
placed in the queue.
8885
'''
@@ -240,8 +237,8 @@ def shutdown(self, immediate=False):
240237
'immediate' to True to make gets raise immediately instead.
241238
242239
All blocked callers of put() and get() will be unblocked. If
243-
'immediate', a task is marked as done for each item remaining in
244-
the queue, which may unblock callers of join().
240+
'immediate', callers of join() are unblocked regardless of
241+
the number of unfinished tasks.
245242
'''
246243
with self.mutex:
247244
self.is_shutdown = True

0 commit comments

Comments
 (0)