gh-96471: Add shutdown() method to queue.Queue (#104750)

Co-authored-by: Duprat <yduprat@gmail.com>
This commit is contained in:
Laurie O 2024-02-10 14:58:30 +10:00 committed by GitHub
parent d4d5bae147
commit b2d9d134dc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 474 additions and 0 deletions

View file

@ -93,6 +93,14 @@ The :mod:`queue` module defines the following classes and exceptions:
on a :class:`Queue` object which is full.
.. exception:: ShutDown
Exception raised when :meth:`~Queue.put` or :meth:`~Queue.get` is called on
a :class:`Queue` object which has been shut down.
.. versionadded:: 3.13
.. _queueobjects:
Queue Objects
@ -135,6 +143,8 @@ provide the public methods described below.
immediately available, else raise the :exc:`Full` exception (*timeout* is
ignored in that case).
Raises :exc:`ShutDown` if the queue has been shut down.
.. method:: Queue.put_nowait(item)
@ -155,6 +165,9 @@ provide the public methods described below.
an uninterruptible wait on an underlying lock. This means that no exceptions
can occur, and in particular a SIGINT will not trigger a :exc:`KeyboardInterrupt`.
Raises :exc:`ShutDown` if the queue has been shut down and is empty, or if
the queue has been shut down immediately.
.. method:: Queue.get_nowait()
@ -177,6 +190,8 @@ fully processed by daemon consumer threads.
Raises a :exc:`ValueError` if called more times than there were items placed in
the queue.
Raises :exc:`ShutDown` if the queue has been shut down immediately.
.. method:: Queue.join()
@ -187,6 +202,8 @@ fully processed by daemon consumer threads.
indicate that the item was retrieved and all work on it is complete. When the
count of unfinished tasks drops to zero, :meth:`join` unblocks.
Raises :exc:`ShutDown` if the queue has been shut down immediately.
Example of how to wait for enqueued tasks to be completed::
@ -214,6 +231,27 @@ Example of how to wait for enqueued tasks to be completed::
print('All work completed')
Terminating queues
^^^^^^^^^^^^^^^^^^
:class:`Queue` objects can be made to prevent further interaction by shutting
them down.
.. method:: Queue.shutdown(immediate=False)
Shut down the queue, making :meth:`~Queue.get` and :meth:`~Queue.put` raise
:exc:`ShutDown`.
By default, :meth:`~Queue.get` on a shut down queue will only raise once the
queue is empty. Set *immediate* to true to make :meth:`~Queue.get` raise
immediately instead.
All blocked callers of :meth:`~Queue.put` will be unblocked. If *immediate*
is true, also unblock callers of :meth:`~Queue.get` and :meth:`~Queue.join`.
.. versionadded:: 3.13
SimpleQueue Objects
-------------------

View file

@ -403,6 +403,13 @@ pdb
command line option or :envvar:`PYTHONSAFEPATH` environment variable).
(Contributed by Tian Gao and Christian Walther in :gh:`111762`.)
queue
-----
* Add :meth:`queue.Queue.shutdown` (along with :exc:`queue.ShutDown`) for queue
termination.
(Contributed by Laurie Opperman and Yves Duprat in :gh:`104750`.)
re
--
* Rename :exc:`!re.error` to :exc:`re.PatternError` for improved clarity.