Skip to content

Commit 9adccc1

Browse files
authored
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
1 parent c5c4281 commit 9adccc1

File tree

5 files changed

+38
-0
lines changed

5 files changed

+38
-0
lines changed

Doc/library/multiprocessing.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,16 @@ For an example of the usage of queues for interprocess communication see
878878

879879
It is a simplified :class:`Queue` type, very close to a locked :class:`Pipe`.
880880

881+
.. method:: close()
882+
883+
Close the queue: release internal resources.
884+
885+
A queue must not be used anymore after it is closed. For example,
886+
:meth:`get`, :meth:`put` and :meth:`empty` methods must no longer be
887+
called.
888+
889+
.. versionadded:: 3.9
890+
881891
.. method:: empty()
882892

883893
Return ``True`` if the queue is empty, ``False`` otherwise.

Doc/whatsnew/3.9.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,14 @@ nntplib
376376
if the given timeout for their constructor is zero to prevent the creation of
377377
a non-blocking socket. (Contributed by Dong-hee Na in :issue:`39259`.)
378378

379+
multiprocessing
380+
---------------
381+
382+
The :class:`multiprocessing.SimpleQueue` class has a new
383+
:meth:`~multiprocessing.SimpleQueue.close` method to explicitly close the
384+
queue.
385+
(Contributed by Victor Stinner in :issue:`30966`.)
386+
379387
os
380388
--
381389

Lib/multiprocessing/queues.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,10 @@ def __init__(self, *, ctx):
346346
else:
347347
self._wlock = ctx.Lock()
348348

349+
def close(self):
350+
self._reader.close()
351+
self._writer.close()
352+
349353
def empty(self):
350354
return not self._poll()
351355

Lib/test/_test_multiprocessing.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5244,6 +5244,20 @@ def test_empty(self):
52445244

52455245
proc.join()
52465246

5247+
def test_close(self):
5248+
queue = multiprocessing.SimpleQueue()
5249+
queue.close()
5250+
# closing a queue twice should not fail
5251+
queue.close()
5252+
5253+
# Test specific to CPython since it tests private attributes
5254+
@test.support.cpython_only
5255+
def test_closed(self):
5256+
queue = multiprocessing.SimpleQueue()
5257+
queue.close()
5258+
self.assertTrue(queue._reader.closed)
5259+
self.assertTrue(queue._writer.closed)
5260+
52475261

52485262
class TestPoolNotLeakOnFailure(unittest.TestCase):
52495263

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add a new :meth:`~multiprocessing.SimpleQueue.close` method to the
2+
:class:`~multiprocessing.SimpleQueue` class to explicitly close the queue.

0 commit comments

Comments
 (0)