Skip to content

Commit

Permalink
Revert "pythongh-96471: Add shutdown() method to queue.Queue (python#…
Browse files Browse the repository at this point in the history
…104750)"

Reason for the revert: python#115258
test_queue timed out on Windows free-threading build.

The PR author had mentioned this in
python#104750 (comment)
but I forgot about it and merged anyway.

This reverts commit b2d9d13.
  • Loading branch information
gvanrossum committed Feb 10, 2024
1 parent 4821f08 commit b431b87
Show file tree
Hide file tree
Showing 5 changed files with 0 additions and 474 deletions.
38 changes: 0 additions & 38 deletions Doc/library/queue.rst
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,6 @@ 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
Expand Down Expand Up @@ -143,8 +135,6 @@ 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)

Expand All @@ -165,9 +155,6 @@ 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()

Expand All @@ -190,8 +177,6 @@ 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()

Expand All @@ -202,8 +187,6 @@ 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::

Expand Down Expand Up @@ -231,27 +214,6 @@ 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
-------------------

Expand Down
7 changes: 0 additions & 7 deletions Doc/whatsnew/3.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -406,13 +406,6 @@ 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.
Expand Down
50 changes: 0 additions & 50 deletions Lib/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ class Full(Exception):
pass


class ShutDown(Exception):
'''Raised when put/get with shut-down queue.'''


class Queue:
'''Create a queue object with a given maximum size.
Expand Down Expand Up @@ -58,9 +54,6 @@ def __init__(self, maxsize=0):
self.all_tasks_done = threading.Condition(self.mutex)
self.unfinished_tasks = 0

# Queue shutdown state
self.is_shutdown = False

def task_done(self):
'''Indicate that a formerly enqueued task is complete.
Expand All @@ -74,8 +67,6 @@ def task_done(self):
Raises a ValueError if called more times than there were items
placed in the queue.
Raises ShutDown if the queue has been shut down immediately.
'''
with self.all_tasks_done:
unfinished = self.unfinished_tasks - 1
Expand All @@ -93,8 +84,6 @@ def join(self):
to indicate the item was retrieved and all work on it is complete.
When the count of unfinished tasks drops to zero, join() unblocks.
Raises ShutDown if the queue has been shut down immediately.
'''
with self.all_tasks_done:
while self.unfinished_tasks:
Expand Down Expand Up @@ -140,21 +129,15 @@ def put(self, item, block=True, timeout=None):
Otherwise ('block' is false), put an item on the queue if a free slot
is immediately available, else raise the Full exception ('timeout'
is ignored in that case).
Raises ShutDown if the queue has been shut down.
'''
with self.not_full:
if self.is_shutdown:
raise ShutDown
if self.maxsize > 0:
if not block:
if self._qsize() >= self.maxsize:
raise Full
elif timeout is None:
while self._qsize() >= self.maxsize:
self.not_full.wait()
if self.is_shutdown:
raise ShutDown
elif timeout < 0:
raise ValueError("'timeout' must be a non-negative number")
else:
Expand All @@ -164,8 +147,6 @@ def put(self, item, block=True, timeout=None):
if remaining <= 0.0:
raise Full
self.not_full.wait(remaining)
if self.is_shutdown:
raise ShutDown
self._put(item)
self.unfinished_tasks += 1
self.not_empty.notify()
Expand All @@ -180,21 +161,14 @@ def get(self, block=True, timeout=None):
Otherwise ('block' is false), return an item if one is immediately
available, else raise the Empty exception ('timeout' is ignored
in that case).
Raises ShutDown if the queue has been shut down and is empty,
or if the queue has been shut down immediately.
'''
with self.not_empty:
if self.is_shutdown and not self._qsize():
raise ShutDown
if not block:
if not self._qsize():
raise Empty
elif timeout is None:
while not self._qsize():
self.not_empty.wait()
if self.is_shutdown and not self._qsize():
raise ShutDown
elif timeout < 0:
raise ValueError("'timeout' must be a non-negative number")
else:
Expand All @@ -204,8 +178,6 @@ def get(self, block=True, timeout=None):
if remaining <= 0.0:
raise Empty
self.not_empty.wait(remaining)
if self.is_shutdown and not self._qsize():
raise ShutDown
item = self._get()
self.not_full.notify()
return item
Expand All @@ -226,28 +198,6 @@ def get_nowait(self):
'''
return self.get(block=False)

def shutdown(self, immediate=False):
'''Shut-down the queue, making queue gets and puts raise.
By default, gets will only raise once the queue is empty. Set
'immediate' to True to make gets raise immediately instead.
All blocked callers of put() will be unblocked, and also get()
and join() if 'immediate'. The ShutDown exception is raised.
'''
with self.mutex:
self.is_shutdown = True
if immediate:
n_items = self._qsize()
while self._qsize():
self._get()
if self.unfinished_tasks > 0:
self.unfinished_tasks -= 1
self.not_empty.notify_all()
# release all blocked threads in `join()`
self.all_tasks_done.notify_all()
self.not_full.notify_all()

# Override these methods to implement other queue organizations
# (e.g. stack or priority queue).
# These will only be called with appropriate locks held
Expand Down
Loading

0 comments on commit b431b87

Please sign in to comment.