bpo-38520: Add multiprocessing.main_process()
Add a multiprocessing.main_process() function to access the main process.
This commit is contained in:
parent
dedc2cd5f0
commit
ab41c231b8
|
@ -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
|
||||
|
|
|
@ -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
|
||||
--
|
||||
|
||||
|
|
|
@ -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'''
|
||||
|
|
|
@ -64,6 +64,8 @@ class DummyProcess(threading.Thread):
|
|||
Process = DummyProcess
|
||||
current_process = threading.current_thread
|
||||
current_process()._children = weakref.WeakKeyDictionary()
|
||||
main_process = threading.main_thread
|
||||
|
||||
|
||||
def active_children():
|
||||
children = current_process()._children
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
#
|
||||
|
||||
__all__ = ['BaseProcess', 'current_process', 'active_children',
|
||||
'parent_process']
|
||||
'parent_process', 'main_process']
|
||||
|
||||
#
|
||||
# Imports
|
||||
|
@ -54,6 +54,13 @@ def parent_process():
|
|||
'''
|
||||
return _parent_process
|
||||
|
||||
|
||||
def main_process():
|
||||
'''
|
||||
Return process object representing the main process
|
||||
'''
|
||||
return _main_process
|
||||
|
||||
#
|
||||
#
|
||||
#
|
||||
|
@ -413,7 +420,7 @@ class _MainProcess(BaseProcess):
|
|||
|
||||
|
||||
_parent_process = None
|
||||
_current_process = _MainProcess()
|
||||
_current_process = _main_process = _MainProcess()
|
||||
_process_counter = itertools.count(1)
|
||||
_children = set()
|
||||
del _MainProcess
|
||||
|
|
|
@ -479,6 +479,18 @@ class _TestProcess(BaseTestCase):
|
|||
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)
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
Add a :func:`multiprocessing.main_process` function to access the main
|
||||
process.
|
Loading…
Reference in New Issue