为什么在 multiprocessing/process.py 中为类 BaseProcess(object) 的 `_parent_pid` 和 `_parent_name` 属性分配当前信息?

问题描述 投票:0回答:1

根据 Python 3.10.12 的

multiprocessing/process.py
文件:

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)

我可以知道为什么第 86 行和 87 行这么写吗:

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

而不是

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

意思是,父属性不应该使用父信息而不是当前信息吗?

python python-multiprocessing
1个回答
0
投票

为什么在 multiprocessing/process.py 中为类 BaseProcess(object) 的

_parent_pid
_parent_name
属性分配当前信息?

因为当前进程正在生成一个新进程。

父属性不应该使用父信息而不是当前信息吗?

不。此外,Python 还被用于数百万个项目。可以肯定地假设标准 python 库中的内容很有可能是正确的。

© www.soinside.com 2019 - 2024. All rights reserved.