網(wǎng)站強(qiáng)制字體wordpress百度推廣基木魚(yú)
前言
????????psutil
是一個(gè)跨平臺(tái)的庫(kù),用于獲取系統(tǒng)的進(jìn)程和系統(tǒng)利用率(包括 CPU、內(nèi)存、磁盤(pán)、網(wǎng)絡(luò)等)信息。
目錄
安裝
應(yīng)用場(chǎng)景
常用方法
一、系統(tǒng)信息相關(guān)函數(shù)
?二、進(jìn)程信息相關(guān)函數(shù)
三、網(wǎng)絡(luò)信息相關(guān)函數(shù)
?四、其他實(shí)用函數(shù)
使用樣例
監(jiān)控應(yīng)用程序
安裝
pip install psutil
應(yīng)用場(chǎng)景
一、系統(tǒng)監(jiān)控
二、進(jìn)程管理
三、資源限制
四、自動(dòng)化任務(wù)
常用方法
一、系統(tǒng)信息相關(guān)函數(shù)
psutil.cpu_times()
:
- 返回系統(tǒng)的 CPU 時(shí)間信息,包括用戶態(tài)時(shí)間、系統(tǒng)態(tài)時(shí)間、空閑時(shí)間等。
psutil.cpu_percent()
:
- 返回 CPU 的使用率百分比??梢灾付ㄩg隔時(shí)間來(lái)獲取一段時(shí)間內(nèi)的平均使用率。
psutil.virtual_memory()
:
- 返回系統(tǒng)的虛擬內(nèi)存信息,包括總內(nèi)存、可用內(nèi)存、已使用內(nèi)存等。
psutil.swap_memory()
:
- 返回系統(tǒng)的交換內(nèi)存信息。
psutil.disk_usage(path)
:
- 返回指定路徑的磁盤(pán)使用情況,包括總空間、已使用空間和可用空間等。
代碼
import psutilcpu_times = psutil.cpu_times()print(f"用戶態(tài)時(shí)間:{cpu_times.user}")print(f"系統(tǒng)態(tài)時(shí)間:{cpu_times.system}")print(f"空閑時(shí)間:{cpu_times.idle}")cpu_percent = psutil.cpu_percent(interval=1)print(f"CPU 使用率:{cpu_percent}%")memory_info = psutil.virtual_memory()print(f"總內(nèi)存:{memory_info.total}")print(f"可用內(nèi)存:{memory_info.available}")print(f"已使用內(nèi)存:{memory_info.used}")swap_info = psutil.swap_memory()print(f"總交換內(nèi)存:{swap_info.total}")print(f"已使用交換內(nèi)存:{swap_info.used}")print(f"可用交換內(nèi)存:{swap_info.free}")disk_usage = psutil.disk_usage('/')print(f"總磁盤(pán)空間:{disk_usage.total}")print(f"已使用磁盤(pán)空間:{disk_usage.used}")print(f"可用磁盤(pán)空間:{disk_usage.free}")
?二、進(jìn)程信息相關(guān)函數(shù)
psutil.pids()
:
- 返回當(dāng)前系統(tǒng)中的所有進(jìn)程 ID。
psutil.Process(pid)
:
- 創(chuàng)建一個(gè)表示特定進(jìn)程的對(duì)象,可以通過(guò)進(jìn)程 ID 來(lái)獲取。
process.name()
:
- 返回進(jìn)程的名稱。
process.exe()
:
- 返回進(jìn)程的可執(zhí)行文件路徑。
process.cpu_percent()
:
- 返回進(jìn)程的 CPU 使用率百分比。
process.memory_info()
:
- 返回進(jìn)程的內(nèi)存使用信息,包括 RSS(常駐集大小)和 VMS(虛擬內(nèi)存大小)等。
process.terminate()
:
- 終止進(jìn)程。
?代碼
pids = psutil.pids()print(f"進(jìn)程 ID 列表:{pids}")process = psutil.Process(1234)process_name = process.name()print(f"進(jìn)程名稱:{process_name}")exe_path = process.exe()print(f"可執(zhí)行文件路徑:{exe_path}")process_cpu_percent = process.cpu_percent(interval=1)print(f"進(jìn)程 CPU 使用率:{process_cpu_percent}%")memory_info = process.memory_info()print(f"進(jìn)程 RSS:{memory_info.rss}")print(f"進(jìn)程 VMS:{memory_info.vms}")process.terminate()
三、網(wǎng)絡(luò)信息相關(guān)函數(shù)
psutil.net_io_counters()
:
- 返回系統(tǒng)的網(wǎng)絡(luò) I/O 統(tǒng)計(jì)信息,包括發(fā)送和接收的字節(jié)數(shù)、數(shù)據(jù)包數(shù)等。
psutil.net_if_addrs()
:
- 返回系統(tǒng)的網(wǎng)絡(luò)接口地址信息。
psutil.net_if_stats()
:
- 返回系統(tǒng)的網(wǎng)絡(luò)接口狀態(tài)信息。
??代碼
net_io = psutil.net_io_counters()print(f"發(fā)送字節(jié)數(shù):{net_io.bytes_sent}")print(f"接收字節(jié)數(shù):{net_io.bytes_recv}")net_addrs = psutil.net_if_addrs()print(f"網(wǎng)絡(luò)接口地址信息:{net_addrs}")net_stats = psutil.net_if_stats()print(f"網(wǎng)絡(luò)接口狀態(tài)信息:{net_stats}")
?四、其他實(shí)用函數(shù)
psutil.boot_time()
:
- 返回系統(tǒng)的啟動(dòng)時(shí)間(以秒為單位,從 Unix 紀(jì)元時(shí)間開(kāi)始計(jì)算)。
psutil.users()
:
- 返回當(dāng)前登錄到系統(tǒng)的用戶信息。
??代碼
boot_time = psutil.boot_time()print(f"系統(tǒng)啟動(dòng)時(shí)間:{boot_time}")users = psutil.users()print(f"當(dāng)前登錄用戶信息:{users}")
使用樣例
監(jiān)控應(yīng)用程序
import psutil
import time# 要監(jiān)控的應(yīng)用程序名稱
target_app_name = "YourAppName.exe"while True:for process in psutil.process_iter(['pid', 'name', 'cpu_percent', 'memory_info']):if process.info['name'] == target_app_name:cpu_percent = process.info['cpu_percent']memory_info = process.info['memory_info']rss_memory = memory_info.rss / (1024 * 1024) # 轉(zhuǎn)換為 MBvms_memory = memory_info.vms / (1024 * 1024) # 轉(zhuǎn)換為 MBprint(f"應(yīng)用程序 {target_app_name}:CPU 使用率 {cpu_percent}%,物理內(nèi)存占用 {rss_memory}MB,虛擬內(nèi)存占用 {vms_memory}MB")time.sleep(5)