Skip to content
Open
Show file tree
Hide file tree
Changes from 15 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 @@ -706,6 +706,12 @@ The :mod:`multiprocessing` package mostly replicates the API of the
>>> p.exitcode == -signal.SIGTERM
True

.. attribute:: closed

Boolean indicating whether the process has been closed via :meth:`close`.

.. versionadded:: 3.14

.. exception:: ProcessError

The base class of all :mod:`multiprocessing` exceptions.
Expand Down
8 changes: 8 additions & 0 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,14 @@ pickle
For more details, please see :ref:`pickle protocols <pickle-protocols>`.


multiprocessing
---------------

* Add attribute :attr:`!closed` to :class:`multiprocessing.Process`,
It is used to check whether the process is closed.
(Contributed by James Roy in :gh:`87063`.)


symtable
--------

Expand Down
7 changes: 7 additions & 0 deletions Lib/multiprocessing/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,13 @@ def is_alive(self):
_children.discard(self)
return False

@property
def closed(self):
'''
Return whether process is closed
'''
return self._closed

def close(self):
'''
Close the Process object.
Expand Down
4 changes: 4 additions & 0 deletions Lib/test/_test_multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,20 +659,24 @@ def test_close(self):
self.assertEqual(p.is_alive(), True)
# Child is still alive, cannot close
with self.assertRaises(ValueError):
self.assertFalse(p.closed)
p.close()
self.assertTrue(p.closed)

q.put(None)
p.join()
self.assertEqual(p.is_alive(), False)
self.assertEqual(p.exitcode, 0)
p.close()
self.assertTrue(p.closed)
with self.assertRaises(ValueError):
p.is_alive()
with self.assertRaises(ValueError):
p.join()
with self.assertRaises(ValueError):
p.terminate()
p.close()
self.assertTrue(p.closed)

wr = weakref.ref(p)
del p
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Allow to determine whether a :class:`multiprocessing.Process` has been closed
via the :attr:`!closed` attribute.
Loading