]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-30966: Add multiprocessing.SimpleQueue.close() (GH-19735)
authorVictor Stinner <vstinner@python.org>
Mon, 27 Apr 2020 16:11:10 +0000 (18:11 +0200)
committerGitHub <noreply@github.com>
Mon, 27 Apr 2020 16:11:10 +0000 (09:11 -0700)
Add a new close() method to multiprocessing.SimpleQueue to explicitly
close the queue.

Automerge-Triggered-By: @pitrou
Doc/library/multiprocessing.rst
Doc/whatsnew/3.9.rst
Lib/multiprocessing/queues.py
Lib/test/_test_multiprocessing.py
Misc/NEWS.d/next/Library/2020-04-27-17-19-09.bpo-30966._5lDx-.rst [new file with mode: 0644]

index ec9521f1fb4a0cc7229bb6332508b63e946d987d..50b90031ab5a58b7f5f740cc4487bd166050da75 100644 (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`.
 
+   .. 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.
index 0b15ec73641bdf10fc4f823422be60729d5de07b..13cd09b0b8be5aa8df5ca2eebbcb5e1cdd48315d 100644 (file)
@@ -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
 --
 
index c0a284d10c8070df9878e08fdcc1bcc0ff899de1..a2901814876d6c0a64c50903e778812b5eace037 100644 (file)
@@ -346,6 +346,10 @@ class SimpleQueue(object):
         else:
             self._wlock = ctx.Lock()
 
+    def close(self):
+        self._reader.close()
+        self._writer.close()
+
     def empty(self):
         return not self._poll()
 
index 083ad536a051c2add358b0bd296a01c99d83acbc..dd894f21f7afcd947f16787e0c0ad4909b6b1359 100644 (file)
@@ -5244,6 +5244,20 @@ class TestSimpleQueue(unittest.TestCase):
 
         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 (file)
index 0000000..14e9e11
--- /dev/null
@@ -0,0 +1,2 @@
+Add a new :meth:`~multiprocessing.SimpleQueue.close` method to the
+:class:`~multiprocessing.SimpleQueue` class to explicitly close the queue.