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 @@ -222,6 +222,14 @@ pickle
For more details, please see :ref:`pickle protocols <pickle-protocols>`.


multiprocessing
rruuaanng marked this conversation as resolved.
Show resolved Hide resolved
---------------

* Add attribute :attr:`!closed` to :class:`multiprocessing.Process`,
It is used to check whether the process is closed.
rruuaanng marked this conversation as resolved.
Show resolved Hide resolved
(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
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:`!closed` attribute.
rruuaanng marked this conversation as resolved.
Show resolved Hide resolved
Loading