サブプロセスpid( )の値がコマンドが返すPopen.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
通常、によって報告されるpidは、によって報告されるpidps
よりも1または2多いですPopen.pid
。