Warum hat der Subprozess pid (Popen.pid
) einen anderen Wert als der ps
Befehl?Subprozess pid unterscheidet sich von ps output
Ich habe dies bemerkt, wenn ps
sowohl von innen Python (mit subprocess.call()
) und von einem anderen Terminal aufgerufen.
Hier ist eine einfache Python-Datei-Test:
#!/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
Normalerweise sind die berichteten pid von ps
1 oder 2 ist mehr als das von Popen.pid
berichtet.
Es funktionierte auf meinem Mac. Sind Sie sicher, dass Sie 'grep command' pid in' ps' nicht sehen? Auf meinem Mac 'ps -A | grep' listet drei Prozesse auf - 'cat','/bin/sh -c ps -A | grep -w Katze', 'grep -w Katze'. – khachik
Ich habe gerade realisiert - vielleicht ist 'Popen.pid' die Shell-Prozess-ID und ps | grep meldet die PID von' Katze'? – khachik
Ja. Es war 'shell = True'. Jemand hatte die Antwort gepostet, aber es scheint nicht mehr hier zu sein ... Um wen es war, bitte repost, damit ich es annehmen kann. – Matthew