使用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上会发生什么?)
答案 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
。...