gpt4 book ai didi

python - Python `multiprocessing` 中进程的 ident 和 pid 有什么区别?

转载 作者:太空宇宙 更新时间:2023-11-04 00:31:02 24 4
gpt4 key购买 nike

使用 multiprocessing.Process 创建和启动的进程具有属性 pid(进程 ID)和 identpid 不也是一个标识符吗?这两者有什么区别?

帮助也没有帮助:

 |  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。总是这样吗?那可能是特定于操作系统的吗? (我在 Linux 上运行它。在 Mac/Windows 上会发生什么情况?)

最佳答案

答案在 source code 中.

cpython/Lib/multiprocessing/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 兼容。根据documentation :

In addition to the threading.Thread API, Process objects also support the following attributes and methods:

pid - Return the process ID. Before the process is spawned, this will be None.

...

关于python - Python `multiprocessing` 中进程的 ident 和 pid 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45860547/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com