系统基础信息模块详解
1.1、系统性能信息模块psutil
psutil是一个跨平台库,能够获取系统的运行进程和系统利用率(包括CPU、内存、磁盘、网络等)
1.1.1、获取系统性能信息
1.CPU信息:
-
user Time 执行用户进程的时间百分比
-
System Time 执行内核进程和中断的时间百分比
-
Wait IO 由于IO等待而使CPU处于idle(空闲)状态的时间百分比
-
Idle,CPU处于idle状态的时间百分比
import psutil
psutil.cpu_times() #使用cpu_times方法获取CPU完整信息,需要显示所有逻辑CPU信息
psutil.cpu_times().user #获取单项数据信息,如用户user的CPU时间比
psutil.cpu_count() #获取CPU的逻辑个数
psutil.cpu_count(logical=False)#获得物理CPU个数
###############################################################################
import psutil
print(psutil.cpu_times())
print(psutil.cpu_times().user)
print(psutil.cpu_count())
print(psutil.cpu_count(logical=False))
2.内存信息:
-
total(内存总数)
-
used (已使用的内存数)
-
free (空闲内存)
-
buffers(缓冲使用数)
-
cache(缓存使用)
-
swap (交换分区使用数)
分别使用psutil.virtual_memory()和psutil.swap_memory()方法获取这些信息。 单位是字节 bytes
转换为GB是 2147479552 / (1024^3) = 2.00 GB
转换为MB是 77897728 / (1024^2) = 74.3 MB
a = psutil.virtual_memory()
print(a)
b = psutil.swap_memory()
print(b)
svmem(total=3075162112, available=1646419968, percent=46.5, used=1221480448, free=87568384, active=1013272576, inactive=1476448256, buffers=185491456, cached=1580621824, shared=1978368, slab=299302912)
sswap(total=2147479552, used=313241600, free=1834237952, percent=14.6, sin=78102528, sout=396525568)
print("总物理内存:{:.2f}GB".format(a[0] / (1024**3)))
print("物理内存已使用:{:.2f}GB".format(a[3] / (1024**3)))
p