Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Doc/library/multiprocessing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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])

Expand Down
8 changes: 8 additions & 0 deletions Doc/whatsnew/3.7.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
--

Expand Down
4 changes: 4 additions & 0 deletions Lib/multiprocessing/queues.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
14 changes: 14 additions & 0 deletions Lib/test/_test_multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down