bpo-30966: Add multiprocessing.SimpleQueue.close() (GH-19735)

Add a new close() method to multiprocessing.SimpleQueue to explicitly
close the queue.

Automerge-Triggered-By: @pitrou
This commit is contained in:
Victor Stinner 2020-04-27 18:11:10 +02:00 committed by GitHub
parent c5c42815ec
commit 9adccc1384
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 38 additions and 0 deletions

View File

@ -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`. 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() .. method:: empty()
Return ``True`` if the queue is empty, ``False`` otherwise. Return ``True`` if the queue is empty, ``False`` otherwise.

View File

@ -376,6 +376,14 @@ nntplib
if the given timeout for their constructor is zero to prevent the creation of 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`.) 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 os
-- --

View File

@ -346,6 +346,10 @@ class SimpleQueue(object):
else: else:
self._wlock = ctx.Lock() self._wlock = ctx.Lock()
def close(self):
self._reader.close()
self._writer.close()
def empty(self): def empty(self):
return not self._poll() return not self._poll()

View File

@ -5244,6 +5244,20 @@ class TestSimpleQueue(unittest.TestCase):
proc.join() 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): class TestPoolNotLeakOnFailure(unittest.TestCase):

View File

@ -0,0 +1,2 @@
Add a new :meth:`~multiprocessing.SimpleQueue.close` method to the
:class:`~multiprocessing.SimpleQueue` class to explicitly close the queue.