繁体   English   中英

终止后获取过程信息

[英]Get Process Information After Termination

终止后,我需要有关一个进程的一些信息。

/proc utime这样的utimeVMPeak就像这样:

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]
每个运行进程都有一个数字子目录; 子目录由进程ID命名。

终止的进程不再运行 ,并且/proc/[pid]目录(包括stat文件) 不再存在

如果将要存在,则可以在调用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()支持的名称的列表,请参见resource.getrusage() .ru_utime.ru_stime都是浮点值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM