psutil
是一个跨平台的 Python 库,用于获取系统使用情况和性能指标。以下是 psutil
可以监测的一些常见硬件指标:
import psutil
cpu_usage = psutil.cpu_percent()
print(f"CPU usage: {cpu_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")
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")
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")
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.")
psutil
还支持其他硬件指标,如风扇速度、功耗等,但需要根据具体硬件和操作系统进行支持。请注意,某些硬件指标的可用性可能因操作系统和设备而异。在使用 psutil
时,请确保已正确安装并导入库,并根据需要调整代码以适应不同的平台和硬件。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:python psutil有哪些应用场景