Processes created and started with multiprocessing.Process
have an attribute pid
(the process id) and ident
. Isn't pid
also an identifier? What is the difference between those two?
The help isn't helping either:
| 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))
In my cases, pid == ident
. Is that always the case? Is that probably OS specific? (I run it on Linux. What happens on Mac / Windows?)
The answer is in the 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 # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
...
Thus multiprocessing.Process.pid
is just an alias for multiprocessing.Process.ident
. Note that multiprocessing.Process
provides the ident
property so that it is compatible with threading.Thread
API. According to 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
....