如何使用python保存内存、pid和进程
import psutil
tasklist=['firefox']
out=[]
for proc in psutil.process_iter():
if any(task in proc.name() for task in tasklist):
out.append([{'pid' : proc.pid, 'name' : proc.name()}])
for o in out[:]:
print(o)
name = [j['name'] for i in out for j in i]
print (name)
检索内存使用情况和CPU使用情况
pmap 413 | tail -n 1
total 2987968K
413是火狐pid
ps -p 413 -o %cpu,%mem
%CPU %MEM
0.1 3.2
如何将内存使用率和CPU使用率添加到字典中 期望的输出
[{'pid': 413, 'name': 'firefox','cpu':0.1, 'mem':3.2 }]
最佳答案
Process
实例有各自的方法来显示 cpu/内存使用百分比:
...
out.append([{'pid' : proc.pid, 'name' : proc.name(),
'cpu': proc.cpu_percent(), 'mem': proc.memory_percent()}])
https://psutil.readthedocs.io/en/latest/#psutil.Process.cpu_percent
关于python - 如何使用python保存内存、pid和进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57496281/