【发布时间】:2019-08-14 14:02:42
【问题描述】:
如何使用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 }]
【问题讨论】:
标签: python dictionary process subprocess