为什么子Popen.pid进程pid()与ps命令返回的值不同?
当我ps从python(with subprocess.call())和另一个终端调用时,我注意到了这一点.
这是一个简单的python文件来测试:
#!/usr/bin/python3
'''
Test subprocess termination
'''
import subprocess
command = 'cat'
#keep pipes so that cat doesn't complain
proc = subprocess.Popen(command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.PIPE,
shell=True)
print('pid = %d' % proc.pid)
subprocess.call("ps -A | grep -w %s" % command,
shell=True)
proc.terminate()
proc.wait() # make sure its dead before exiting pytyhon
Run Code Online (Sandbox Code Playgroud)
通常报告的pid比报告的pid ps多1或2 Popen.pid.