[Python-checkins] bpo-30966: Add multiprocessing.SimpleQueue.close() (GH-19735)

Victor Stinner webhook-mailer at python.org
Mon Apr 27 12:11:21 EDT 2020


https://github.com/python/cpython/commit/9adccc1384568f4d46e37f698cb3e3a4f6ca0252
commit: 9adccc1384568f4d46e37f698cb3e3a4f6ca0252
branch: master
author: Victor Stinner <vstinner at python.org>
committer: GitHub <noreply at github.com>
date: 2020-04-27T09:11:10-07:00
summary:

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

files:
A Misc/NEWS.d/next/Library/2020-04-27-17-19-09.bpo-30966._5lDx-.rst
M Doc/library/multiprocessing.rst
M Doc/whatsnew/3.9.rst
M Lib/multiprocessing/queues.py
M Lib/test/_test_multiprocessing.py

diff --git a/Doc/library/multiprocessing.rst b/Doc/library/multiprocessing.rst
index ec9521f1fb4a0..50b90031ab5a5 100644
--- a/Doc/library/multiprocessing.rst
+++ b/Doc/library/multiprocessing.rst
@@ -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.
diff --git a/Doc/whatsnew/3.9.rst b/Doc/whatsnew/3.9.rst
index 0b15ec73641bd..13cd09b0b8be5 100644
--- a/Doc/whatsnew/3.9.rst
+++ b/Doc/whatsnew/3.9.rst
@@ -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
 --
 
diff --git a/Lib/multiprocessing/queues.py b/Lib/multiprocessing/queues.py
index c0a284d10c807..a2901814876d6 100644
--- a/Lib/multiprocessing/queues.py
+++ b/Lib/multiprocessing/queues.py
@@ -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()
 
diff --git a/Lib/test/_test_multiprocessing.py b/Lib/test/_test_multiprocessing.py
index 083ad536a051c..dd894f21f7afc 100644
--- a/Lib/test/_test_multiprocessing.py
+++ b/Lib/test/_test_multiprocessing.py
@@ -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):
 
diff --git a/Misc/NEWS.d/next/Library/2020-04-27-17-19-09.bpo-30966._5lDx-.rst b/Misc/NEWS.d/next/Library/2020-04-27-17-19-09.bpo-30966._5lDx-.rst
new file mode 100644
index 0000000000000..14e9e11538763
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-04-27-17-19-09.bpo-30966._5lDx-.rst
@@ -0,0 +1,2 @@
+Add a new :meth:`~multiprocessing.SimpleQueue.close` method to the
+:class:`~multiprocessing.SimpleQueue` class to explicitly close the queue.



More information about the Python-checkins mailing list