Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -970,6 +970,12 @@ Miscellaneous

.. versionadded:: 3.8

.. function:: main_process()

Return the main :class:`Process` object.

.. versionadded:: 3.10

.. function:: freeze_support()

Add support for when a program which uses :mod:`multiprocessing` has been
Expand Down
7 changes: 7 additions & 0 deletions Doc/whatsnew/3.10.rst
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,13 @@ linecache
When a module does not define ``__loader__``, fall back to ``__spec__.loader``.
(Contributed by Brett Cannon in :issue:`42133`.)

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

Add a new :func:`multiprocessing.main_process` function to access the main
process.
(Contributed by Zackery Spytz in :issue:`38520`.)

os
--

Expand Down
1 change: 1 addition & 0 deletions Lib/multiprocessing/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class BaseContext(object):
current_process = staticmethod(process.current_process)
parent_process = staticmethod(process.parent_process)
active_children = staticmethod(process.active_children)
main_process = staticmethod(process.main_process)

def cpu_count(self):
'''Returns the number of CPUs in the system'''
Expand Down
2 changes: 2 additions & 0 deletions Lib/multiprocessing/dummy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ def exitcode(self):
Process = DummyProcess
current_process = threading.current_thread
current_process()._children = weakref.WeakKeyDictionary()
main_process = threading.main_thread


def active_children():
children = current_process()._children
Expand Down
11 changes: 9 additions & 2 deletions Lib/multiprocessing/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#

__all__ = ['BaseProcess', 'current_process', 'active_children',
'parent_process']
'parent_process', 'main_process']

#
# Imports
Expand Down Expand Up @@ -54,6 +54,13 @@ def parent_process():
'''
return _parent_process


def main_process():
'''
Return process object representing the main process
'''
return _main_process

#
#
#
Expand Down Expand Up @@ -413,7 +420,7 @@ def close(self):


_parent_process = None
_current_process = _MainProcess()
_current_process = _main_process = _MainProcess()
_process_counter = itertools.count(1)
_children = set()
del _MainProcess
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 @@ -479,6 +479,18 @@ def test_active_children(self):
p.join()
self.assertNotIn(p, self.active_children())

@classmethod
def _test_main_process(cls, conn):
conn.send(cls.main_process() is cls.current_process())

def test_main_process(self):
self.assertIs(self.main_process(), self.current_process())
reader, writer = self.Pipe(duplex=False)
p = self.Process(target=self._test_main_process, args=(writer,))
p.start()
p.join()
self.assertEqual(reader.recv(), False)

@classmethod
def _test_recursion(cls, wconn, id):
wconn.send(id)
Expand Down Expand Up @@ -5634,6 +5646,7 @@ class ProcessesMixin(BaseMixin):
current_process = staticmethod(multiprocessing.current_process)
parent_process = staticmethod(multiprocessing.parent_process)
active_children = staticmethod(multiprocessing.active_children)
main_process = staticmethod(multiprocessing.main_process)
Pool = staticmethod(multiprocessing.Pool)
Pipe = staticmethod(multiprocessing.Pipe)
Queue = staticmethod(multiprocessing.Queue)
Expand Down Expand Up @@ -5718,6 +5731,7 @@ class ThreadsMixin(BaseMixin):
connection = multiprocessing.dummy.connection
current_process = staticmethod(multiprocessing.dummy.current_process)
active_children = staticmethod(multiprocessing.dummy.active_children)
main_process = staticmethod(multiprocessing.dummy.main_process)
Pool = staticmethod(multiprocessing.dummy.Pool)
Pipe = staticmethod(multiprocessing.dummy.Pipe)
Queue = staticmethod(multiprocessing.dummy.Queue)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add a :func:`multiprocessing.main_process` function to access the main
process.