我需要一些有关终止后子进程的信息。
比如/proc
中的utime
和VMPeak
是这样的:
proc.wait()
with open('/proc/%d/stat' % proc.pid, "r") as f:
stat = str(f.read()).strip().split()
# 14th column is utime, 15th column is stime (man 5 proc)
cpu_time = int(stat[14]) + int(stat[15])
return cpu_time
但是 Python Popen.wait()
释放了 PID
,所以我会得到:
No such file or directory
我可以在终止后,或者等待终止而不释放PID
吗? (我的意思是等待终止没有调用wait()
,这将释放所有资源。)
如果你能帮助我,我将不胜感激。谢谢!
最佳答案
引用自man 5 proc
:
/proc/[pid]
There is a numerical subdirectory for each running process; the subdirectory is named by the process ID.
终止的进程不再运行,其/proc/[pid]
目录(包括stat
文件)是< em>不再存在。
如果它要在那里,您可以在 调用 proc.wait()
之前将 PID 存储在变量中:
pid = proc.pid
proc.wait()
with open('/proc/%d/stat' % pid, "r") as f:
没有“就在终止前”事件; subprocess
等待,然后获取退出代码。
您可以使用 os.wait4()
进行自己的等待相反:
pid, status, resources = os.wait4(proc.pid, 0)
cpu_time = resources.ru_utime + resources.ru_stime
resources
是一个命名元组,参见 resource.getrusage()
有关它支持的名称的列表。 .ru_utime
和.ru_stime
都是 float 。
关于python - 终止后获取进程信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17906151/