Skip to content

Commit cfd65b8

Browse files
committed
Fix docs for Queue.shutdown
1 parent b6d3242 commit cfd65b8

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

Doc/library/queue.rst

Lines changed: 31 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

@@ -233,22 +230,42 @@ Example of how to wait for enqueued tasks to be completed::
233230
Terminating queues
234231
^^^^^^^^^^^^^^^^^^
235232

236-
:class:`Queue` objects can be made to prevent further interaction by shutting
237-
them down.
233+
When no longer needed, :class:`Queue` objects can be wound down
234+
or terminated immediately.
238235

239236
.. method:: Queue.shutdown(immediate=False)
240237

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

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.
259+
If *immediate* is true, the queue is terminated immediately.
260+
The queue is drained to be completely empty. The count of
261+
unfinished tasks is reduced to zero but without calling
262+
:meth:`~Queue.task_done`. That then unblocks all callers of
263+
:meth:`~Queue.join`. In addition, blocked callers of
264+
:meth:`~Queue.get` are unblocked and will raise :exc:`ShutDown`.
247265

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`.
266+
Use caution when using :meth:`~Queue.join` with *immediate* set
267+
to true. This unblocks the join even when no work has been done
268+
on the tasks, violating the usual invariant for joining a queue.
252269

253270
.. versionadded:: 3.13
254271

0 commit comments

Comments
 (0)