Pythons“multiprocessing”中的ident和pid有什么区别?

时间:2017-08-24 11:33:51

标签: python multiprocessing

使用multiprocessing.Process创建并启动的流程具有属性pid(流程ID)和ident。 Isn&#t; t pid也是一个标识符?这两者有什么区别?

帮助也没有帮助:

 |  ident
 |      Return identifier (PID) of process or `None` if it has yet to start
 |  
 |  pid
 |      Return identifier (PID) of process or `None` if it has yet to start

实施例

import multiprocessing
import time


def whatever():
    print("child")
    time.sleep(10)
    print("child done")

created = multiprocessing.Process(target=whatever)
created.start()
time.sleep(1)
print("pid={}".format(created.pid))
print("ident={}".format(created.ident))

就我而言,pid == ident。总是这样吗?那可能是OS特有的吗? (我在Linux上运行它。在Mac / Windows上会发生什么?)

1 个答案:

答案 0 :(得分:3)

答案在source code

<强>的CPython / LIB /多/ process.py:

class BaseProcess(object):

    ...

    @property
    def ident(self):
        '''
        Return identifier (PID) of process or `None` if it has yet to start
        '''
        self._check_closed()
        if self is _current_process:
            return os.getpid()
        else:
            return self._popen and self._popen.pid

    pid = ident  # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    ...

因此multiprocessing.Process.pid只是multiprocessing.Process.ident的别名。请注意,multiprocessing.Process提供ident属性,以便与threading.Thread API兼容。根据{{​​3}}:

  

除了   documentation   API,   threading.Thread   对象还支持以下属性和方法:

     

pid - 返回进程ID。在生成流程之前,这将是None

     

...