[英]Get Process Information After Termination
終止后,我需要有關一個子進程的一些信息。
如/proc
utime
這樣的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]
每個運行進程都有一個數字子目錄; 子目錄由進程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.