Python的psutil庫是一個跨平臺的庫,用于獲取有關正在運行的進程和系統利用率(CPU、內存、磁盤、網絡等)的信息
pip install psutil
import psutil
cpu_info = psutil.cpu_percent()
print(f"CPU Usage: {cpu_info}%")
memory_info = psutil.virtual_memory()
print(f"Total Memory: {memory_info.total / (1024 * 1024)} MB")
print(f"Available Memory: {memory_info.available / (1024 * 1024)} MB")
disk_usage = psutil.disk_usage('/')
print(f"Total Disk Space: {disk_usage.total / (1024 * 1024 * 1024)} GB")
print(f"Used Disk Space: {disk_usage.used / (1024 * 1024 * 1024)} GB")
net_io = psutil.net_io_counters()
print(f"Bytes Sent: {net_io.bytes_sent}")
print(f"Bytes Received: {net_io.bytes_recv}")
processes = psutil.process_iter(['pid', 'name', 'cpu_percent'])
for process in processes:
print(f"PID: {process.info['pid']}, Name: {process.info['name']}, CPU Usage: {process.info['cpu_percent']}%")
import time
while True:
cpu_usage = psutil.cpu_percent()
print(f"CPU Usage: {cpu_usage}%")
time.sleep(1)
這只是psutil庫的一些基本用法。您可以根據需要使用這些功能來監控和獲取系統資源信息。要了解更多關于psutil庫的信息,請查閱官方文檔:https://psutil.readthedocs.io/en/latest/