通常我使用:
os.popen("du folder >> 1.txt ").read()
它工作正常。
但是当我想获取子进程 id 时,它返回空值。
os.popen("du folder >> 1.txt &").read() # Notice the & symbol
有谁知道为什么以及如何获得PID?
您将需要使用该subprocess
模块。
# Can't use shell=True if you want the pid of `du`, not the
# shell, so we have to do the redirection to file ourselves
proc = subprocess.Popen("/usr/bin/du folder", stdout=file("1.txt", "ab"))
print "PID:", proc.pid
print "Return code:", proc.wait()
&
将进程置于后台和作业编号!= pid。获取进程的 pid。
我建议使用subprocess -Popen
实例具有pid
您可以直接访问的属性。