diff --git a/Doc/library/multiprocessing.rst b/Doc/library/multiprocessing.rst index 9318a750919b67f..7a3786eff17312c 100644 --- a/Doc/library/multiprocessing.rst +++ b/Doc/library/multiprocessing.rst @@ -862,6 +862,12 @@ For an example of the usage of queues for interprocess communication see Put *item* into the queue. + .. method:: close() + + Close the queue. + + .. versionadded:: 3.7 + .. class:: JoinableQueue([maxsize]) diff --git a/Doc/whatsnew/3.7.rst b/Doc/whatsnew/3.7.rst index 023d0a1b238089d..14e6f65aec0ded6 100644 --- a/Doc/whatsnew/3.7.rst +++ b/Doc/whatsnew/3.7.rst @@ -210,6 +210,14 @@ math New :func:`~math.remainder` function, implementing the IEEE 754-style remainder operation. (Contributed by Mark Dickinson in :issue:`29962`.) + +multiprocessing +--------------- + +The :class:`multiprocessing.SimpleQueue` class has a new +:meth:`~multiprocessing.SimpleQueue.close` method to explicitly close the +queue. + os -- diff --git a/Lib/multiprocessing/queues.py b/Lib/multiprocessing/queues.py index 513807cafecb6dd..e302e7c6959e3ba 100644 --- a/Lib/multiprocessing/queues.py +++ b/Lib/multiprocessing/queues.py @@ -319,6 +319,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 0515730d14a853f..0ecf9aec5d0c9a8 100644 --- a/Lib/test/_test_multiprocessing.py +++ b/Lib/test/_test_multiprocessing.py @@ -4236,6 +4236,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) + # # Mixins #