【发布时间】:2013-10-03 06:16:04
【问题描述】:
我通常使用:
os.popen("du folder >> 1.txt ").read()
一切正常。
但是当我想获取子进程id时,它返回的是空值。
os.popen("du folder >> 1.txt &").read() # Notice the & symbol
有人知道为什么以及如何获取 PID 吗?
【问题讨论】:
我通常使用:
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()
【讨论】:
du 命令可能在其他地方。试试which du 看看在哪里。
& 将进程置于后台,作业号 != pid。获取进程的 pid。
我建议使用subprocess - Popen 实例具有属性pid,您可以直接访问。
【讨论】: