psutil
是一個 Python 庫,用于獲取系統(tǒng)使用情況(如 CPU、內(nèi)存、磁盤、網(wǎng)絡(luò)等)的實(shí)時信息。它可以幫助你簡化開發(fā)過程,提高生產(chǎn)力。以下是如何使用 psutil
簡化開發(fā)的幾個示例:
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 / (1024 * 1024)} MB")
print(f"Available memory: {memory_info.available / (1024 * 1024)} MB")
import psutil
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")
import psutil
net_io_counters = psutil.net_io_counters()
print(f"Bytes sent: {net_io_counters.bytes_sent}")
print(f"Bytes received: {net_io_counters.bytes_recv}")
import psutil
import time
while True:
cpu_usage = psutil.cpu_percent()
memory_info = psutil.virtual_memory()
print(f"CPU usage: {cpu_usage}%")
print(f"Available memory: {memory_info.available / (1024 * 1024)} MB")
time.sleep(5)
通過使用 psutil
,你可以輕松獲取系統(tǒng)資源使用情況,從而更好地監(jiān)控和調(diào)整你的應(yīng)用程序。此外,它還提供了許多其他功能,如進(jìn)程管理、文件 I/O 統(tǒng)計(jì)等,可以根據(jù)你的需求進(jìn)行進(jìn)一步探索。