温馨提示×

psutil python能监测哪些硬件指标

小樊
89
2024-12-06 20:38:19
栏目: 编程语言
Python开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

psutil 是一个跨平台的 Python 库,用于获取系统使用情况和性能指标。以下是 psutil 可以监测的一些常见硬件指标:

  1. CPU 使用率(CPU usage):获取系统或特定进程的 CPU 使用情况,以百分比表示。
import psutil

cpu_usage = psutil.cpu_percent()
print(f"CPU usage: {cpu_usage}%")
  1. 内存使用情况(Memory usage):获取系统的内存使用情况,包括总内存、已使用内存、空闲内存等。
import psutil

memory_info = psutil.virtual_memory()
print(f"Total memory: {memory_info.total} bytes")
print(f"Used memory: {memory_info.used} bytes")
print(f"Free memory: {memory_info.free} bytes")
  1. 磁盘使用情况(Disk usage):获取系统的磁盘空间使用情况,包括总磁盘空间、已使用磁盘空间、空闲磁盘空间等。
import psutil

disk_usage = psutil.disk_usage('/')
print(f"Total disk space: {disk_usage.total} bytes")
print(f"Used disk space: {disk_usage.used} bytes")
print(f"Free disk space: {disk_usage.free} bytes")
  1. 网络接口使用情况(Network interface usage):获取系统的网络接口使用情况,包括发送和接收的字节数、数据包数等。
import psutil

net_io_counters = psutil.net_io_counters()
print(f"Bytes sent: {net_io_counters.bytes_sent} bytes")
print(f"Bytes received: {net_io_counters.bytes_recv} bytes")
  1. CPU 温度(CPU temperature):获取系统的 CPU 温度(仅适用于支持硬件监控芯片的设备)。
import psutil

try:
    cpu_temps = psutil.sensors_temperatures()
    for name, entries in cpu_temps.items():
        for entry in entries:
            print(f"{name}: {entry.current}°C")
except NotImplementedError:
    print("CPU temperature monitoring is not supported on this platform.")
  1. 其他硬件指标:psutil 还支持其他硬件指标,如风扇速度、功耗等,但需要根据具体硬件和操作系统进行支持。

请注意,某些硬件指标的可用性可能因操作系统和设备而异。在使用 psutil 时,请确保已正确安装并导入库,并根据需要调整代码以适应不同的平台和硬件。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:python psutil有哪些应用场景

0