Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Doc/library/multiprocessing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,16 @@ For an example of the usage of queues for interprocess communication see

It is a simplified :class:`Queue` type, very close to a locked :class:`Pipe`.

.. method:: close()

Close the queue: release internal resources.

A queue must not be used anymore after it is closed. For example,
:meth:`get`, :meth:`put` and :meth:`empty` methods must no longer be
called.

.. versionadded:: 3.9

.. method:: empty()

Return ``True`` if the queue is empty, ``False`` otherwise.
Expand Down
8 changes: 8 additions & 0 deletions Doc/whatsnew/3.9.rst
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,14 @@ nntplib
if the given timeout for their constructor is zero to prevent the creation of
a non-blocking socket. (Contributed by Dong-hee Na in :issue:`39259`.)

multiprocessing
---------------

The :class:`multiprocessing.SimpleQueue` class has a new
:meth:`~multiprocessing.SimpleQueue.close` method to explicitly close the
queue.
(Contributed by Victor Stinner in :issue:`30966`.)

os
--

Expand Down
4 changes: 4 additions & 0 deletions Lib/multiprocessing/queues.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,10 @@ def __init__(self, *, ctx):
else:
self._wlock = ctx.Lock()

def close(self):
self._reader.close()
self._writer.close()

def empty(self):
return not self._poll()

Expand Down
14 changes: 14 additions & 0 deletions Lib/test/_test_multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5244,6 +5244,20 @@ def test_empty(self):

proc.join()

def test_close(self):
queue = multiprocessing.SimpleQueue()
queue.close()
# closing a queue twice should not fail
queue.close()

# Test specific to CPython since it tests private attributes
@test.support.cpython_only
def test_closed(self):
queue = multiprocessing.SimpleQueue()
queue.close()
self.assertTrue(queue._reader.closed)
self.assertTrue(queue._writer.closed)


class TestPoolNotLeakOnFailure(unittest.TestCase):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add a new :meth:`~multiprocessing.SimpleQueue.close` method to the
:class:`~multiprocessing.SimpleQueue` class to explicitly close the queue.