【问题标题】:How to save the memory, pid and process using python如何使用python保存内存、pid和进程
【发布时间】: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


    【解决方案1】:

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-24
      • 2010-11-29
      • 1970-01-01
      • 2016-11-24
      • 1970-01-01
      • 2010-10-17
      • 2015-01-29
      相关资源
      最近更新 更多