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 15 commits into
base: main
Choose a base branch
from
9 changes: 9 additions & 0 deletions Doc/library/multiprocessing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,15 @@ The :mod:`multiprocessing` package mostly replicates the API of the

.. versionadded:: 3.7

.. method:: closed()
rruuaanng marked this conversation as resolved.
Show resolved Hide resolved

Return whether the process is closed.

Roughly, a process object is considered closed when :meth:`close`
is called on it.
rruuaanng marked this conversation as resolved.
Show resolved Hide resolved

.. versionadded:: 3.14

.. method:: close()

Close the :class:`Process` object, releasing all resources associated
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
15 changes: 15 additions & 0 deletions Lib/test/_test_multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,21 @@ def _test_close(cls, rc=0, q=None):
q.get()
sys.exit(rc)

def test_is_close(self):
rruuaanng marked this conversation as resolved.
Show resolved Hide resolved
if self.TYPE == "threads":
self.skipTest('test not appropriate for {}'.format(self.TYPE))
def _test_task():
pass
p = self.Process(target=_test_task)
self.assertFalse(p.closed)
p.close()
self.assertTrue(p.closed)

wr = weakref.ref(p)
del p
gc.collect()
self.assertIs(wr(), None)

def test_close(self):
if self.TYPE == "threads":
self.skipTest('test not appropriate for {}'.format(self.TYPE))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add the ``closed()`` in the :class:`multiprocessing.Process` to
rruuaanng marked this conversation as resolved.
Show resolved Hide resolved
determine whether the process is closed.
Loading