python - 如何使用python保存内存、pid和进程
问题描述
如何使用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
推荐阅读
- android - Android - 用于重复 maven 项目存储库模板的 gradle 脚本或函数
- python - 比较合并和插入排序的问题
- r - Agricolae,tapply,错误:参数必须具有相同的长度
- r - 如何在R中的文本中查找ID列表
- spring - @ApiOperation 注解在 spring 中是如何工作的?
- azure - 使用 Azure Migrate 将机器迁移到开发测试实验室
- excel - excel中的自定义格式单元格
- django - Django:身份验证凭据未提供错误,因为访问和刷新令牌设置为没有授权标头的 HttpOnly cookie
- javascript - 使用 react-chartjs-2 对多个图表进行分组
- unity3d - 在 Unity 上构建 android 应用程序时 gradle build 失败