Search code examples
pythonpython-multiprocessing

Why are `_parent_pid` and `_parent_name` attributes of class BaseProcess(object) in multiprocessing/process.py assigned with current info?


According to the multiprocessing/process.py file for Python 3.10.12:

class BaseProcess(object):
    '''
    Process objects represent activity that is run in a separate process

    The class is analogous to `threading.Thread`
    '''
    def _Popen(self):
        raise NotImplementedError

    def __init__(self, group=None, target=None, name=None, args=(), kwargs={},
                 *, daemon=None):
        assert group is None, 'group argument must be None for now'
        count = next(_process_counter)
        self._identity = _current_process._identity + (count,)
        self._config = _current_process._config.copy()
        self._parent_pid = os.getpid()
        self._parent_name = _current_process.name
        self._popen = None
        self._closed = False
        self._target = target
        self._args = tuple(args)
        self._kwargs = dict(kwargs)
        self._name = name or type(self).__name__ + '-' + \
                     ':'.join(str(i) for i in self._identity)
        if daemon is not None:
            self.daemon = daemon
        _dangling.add(self)

May I know why lines 86 & 87 write this:

self._parent_pid = os.getpid()
self._parent_name = _current_process.name 

and not

self._parent_pid = os.getppid()
self._parent_name = _parent_process.name  

Meaning, should not the parent info instead of the current info be used for the parent attributes?


Solution

  • Why are _parent_pid and _parent_name attributes of class BaseProcess(object) in multiprocessing/process.py assigned with current info?

    Because the current process is spawning a new process.

    should not the parent info instead of the current info be used for the parent attributes?

    No. Additionally, Python is used in millions of projects. It is safe to assume that with high probability what is in the standard python library is correct.