From 9adccc1384568f4d46e37f698cb3e3a4f6ca0252 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 27 Apr 2020 18:11:10 +0200 Subject: 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 --- Doc/library/multiprocessing.rst | 10 ++++++++++ Doc/whatsnew/3.9.rst | 8 ++++++++ Lib/multiprocessing/queues.py | 4 ++++ Lib/test/_test_multiprocessing.py | 14 ++++++++++++++ .../next/Library/2020-04-27-17-19-09.bpo-30966._5lDx-.rst | 2 ++ 5 files changed, 38 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2020-04-27-17-19-09.bpo-30966._5lDx-.rst diff --git a/Doc/library/multiprocessing.rst b/Doc/library/multiprocessing.rst index ec9521f..50b9003 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 0b15ec7..13cd09b 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 c0a284d..a290181 100644 --- a/Lib/multiprocessing/queues.py +++ b/Lib/multiprocessing/queues.py @@ -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() diff --git a/Lib/test/_test_multiprocessing.py b/Lib/test/_test_multiprocessing.py index 083ad53..dd894f2 100644 --- a/Lib/test/_test_multiprocessing.py +++ b/Lib/test/_test_multiprocessing.py @@ -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 index 0000000..14e9e11 --- /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. -- cgit v0.12