Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-87063: Add closed attribute to multiprocessing.Process #125838

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
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 @@ -171,6 +171,14 @@ See the :ref:`JSON command-line interface <json-commandline>` documentation.
(Contributed by Trey Hunner in :gh:`122873`.)


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

* Add the :attr:`~multiprocessing.Process.closed` property to
:class:`multiprocessing.Process` objects to determine whether
they have been closed or not.

rruuaanng marked this conversation as resolved.
Show resolved Hide resolved

operator
--------

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
picnixz marked this conversation as resolved.
Show resolved Hide resolved

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)
rruuaanng marked this conversation as resolved.
Show resolved Hide resolved

q.put(None)
p.join()
self.assertEqual(p.is_alive(), False)
self.assertEqual(p.exitcode, 0)
p.close()
self.assertTrue(p.closed)
rruuaanng marked this conversation as resolved.
Show resolved Hide resolved
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:`~multiprocessing.Process.closed` property.
rruuaanng marked this conversation as resolved.
Show resolved Hide resolved
Loading