psutil模块

psutil 模块

import psutil

# 逻辑核心
cpu_no = psutil.cpu_count()
# 8

# 物理核心
cpu_rel_no = psutil.cpu_count(logical=False)
# 4

# 统计CPU的用户/系统/空闲时间
cpu_time = psutil.cpu_times()
# scputimes(user=130298.14, nice=0.0, system=145884.58, idle=5131330.82)

# CPU使用率,每秒刷新一次,累计10次   类似于top
for i in range(1):
    # 返回CPU的利用率, percpu为True时显示所有物理核心的利用率, interval不为0时, 则阻塞时显示interval执行的时间内的平均利用率
    cpu_percent = psutil.cpu_percent(percpu=True, interval=2)
    # [27.1, 8.5, 24.0, 18.6, 27.9, 24.0, 46.2, 46.5]

# 物理内存
rel_memory = psutil.virtual_memory()
# svmem(total=17179869184, available=7014621184, percent=59.2, used=8842629120, free=358690816, active=6656806912, inactive=6598430720, wired=2185822208)

# 交换内存
swap_memory = psutil.swap_memory()
# sswap(total=1073741824, used=245628928, free=828112896, percent=22.9, sin=61215977472, sout=161824768)

# 用户
user = psutil.users()
# [suser(name='xxx', terminal=None, host=None, started=1645511653.3969078, pid=None)]
posted @ 2020-04-10 22:18  疯狂列表推导式  阅读(103)  评论(0)    收藏  举报