Python利用psutil獲取CPU與內(nèi)存等硬件信息
psutil是Python的一個(gè)第三方庫,提供了各種強(qiáng)大的硬件信息查閱功能,是標(biāo)準(zhǔn)庫推薦的第三方庫。一般conda會(huì)自行攜帶這個(gè)模塊,如果未安裝,可直接pip
pip install psutil
CPU 相關(guān)
| 函數(shù) | 功能 | |
|---|---|---|
| cpu_count() | CPU核心數(shù)/線程數(shù) | |
| cpu_freq() | CPU頻率 | |
| cpu_times() | CPU時(shí)間 | |
| cpu_stats() | CPU統(tǒng)計(jì)信息 | |
| cpu_percent() | CPU使用率 |
以i9-13900H為例,是一個(gè)6大核8小核總計(jì)20線程的CPU,則其核心數(shù)和線程數(shù)為
import psutil print(psutil.cpu_count(logical=False)) # 返回核心數(shù)14 print(psutil.cpu_count()) # 進(jìn)程數(shù)20 print(psutil.cpu_freq()) # CPU頻率 # scpufreq(current=2600.0, min=0.0, max=2600.0)
cpu_times和cpu_percent功能類似,都是統(tǒng)計(jì)CPU的時(shí)間占用,前者主要統(tǒng)計(jì)CPU 的用戶/系統(tǒng)/空閑時(shí)間;后者則返回某個(gè)時(shí)間段內(nèi)CPU的占用比例,可通過interval來調(diào)節(jié)統(tǒng)計(jì)的延時(shí)。二者均提供percpu參數(shù),為True時(shí),將返回每個(gè)線程的占用情況。
print(psutil.cpu_times()) # scputimes(user=217520.578125, system=74740.26562500186, idle=8452631.640624998, interrupt=3238.703125, dpc=2513.390625) # 0.5秒內(nèi)每個(gè)現(xiàn)成的資源利用占比 print(psutil.cpu_percent(interval=0.5, percpu=True)) # [0.0, 0.0, 0.0, 0.0, 0.0, 2.9, 11.8, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 24.2, 0.0, 0.0, 0.0, 32.3, 0.0, 0.0]
通過cpu_stats函數(shù)可查看CPU 的統(tǒng)計(jì)信息,包括上下文切換、中斷、軟中斷,以及系統(tǒng)調(diào)用次數(shù)等等
print(psutil.cpu_stats()) # scpustats(ctx_switches=3751269358, interrupts=3638215162, soft_interrupts=0, syscalls=443301569)
內(nèi)存相關(guān)
| 函數(shù) | 功能 |
|---|---|
| virtual_memory() | 內(nèi)存利用情況 |
| swap_memory() | 交換內(nèi)存 |
這兩個(gè)函數(shù)的返回值均有下面幾個(gè)屬性
- total 總內(nèi)存
- used 已使用的內(nèi)存
- percent 內(nèi)存使用率
此外,virtual_memory返回值中有available屬性,表示可用內(nèi)存,示例如下
vm = psutil.virtual_memory() GB = 1024*1024*1024 print(vm.total/GB, "GB") # 31.741661071777344 GB print(vm.available/GB, "GB") # 15.915771484375 GB print(vm.percent, "%") # 49.9 % print(vm.used/GB, "GB") # 15.825889587402344 GB print(psutil.swap_memory()) # 返回交換內(nèi)存 # sswap(total=5100273664, used=11184832512, free=-6084558848, percent=219.3, sin=0, sout=0)
硬盤相關(guān)
| 函數(shù) | 說明 |
|---|---|
| disk_partitions() | 返回分區(qū)情況 |
| disk_usage() | 輸入盤符,返回磁盤占用情況 |
| disk_io_counters() | 磁盤的讀寫信息 |
psutil.disk_usage("C:") # 查看C盤的讀寫情況,單位是B
# sdiskusage(total=511282507776, used=307277705216, free=204004802560, percent=60.1)
parts = psutil.disk_partitions()
for p in parts: print(p)
'''
sdiskpart(device='C:\\', mountpoint='C:\\', fstype='NTFS', opts='rw,fixed', maxfile=255, maxpath=260)
sdiskpart(device='D:\\', mountpoint='D:\\', fstype='NTFS', opts='rw,fixed', maxfile=255, maxpath=260)
'''disk_io_counters函數(shù)中,perdisk為True時(shí),返回每個(gè)磁盤的讀寫信息,否則返回所有磁盤相加之后的信息,示例如下
print(psutil.disk_io_counters(perdisk=True))
'''
{'PhysicalDrive0': sdiskio(read_count=4063907, write_count=12341332, read_bytes=162591043584, write_bytes=235670665216, read_time=6212, write_time=10800), 'PhysicalDrive1': sdiskio(read_count=782479, write_count=332042, read_bytes=55565715968, write_bytes=9277476864, read_time=897, write_time=308)}
'''電源相關(guān)
| 函數(shù) | 返回值 |
|---|---|
| sensors_temperatures() | 溫度 |
| sensors_fans() | 風(fēng)扇轉(zhuǎn)速 |
| sensors_battery() | 電源狀態(tài) |
其中CPU溫度和風(fēng)扇轉(zhuǎn)速并不適用于Widows系統(tǒng),故而下面只測試一下sensors_battery函數(shù)
>>> psutil.sensors_battery() sbattery(percent=100, secsleft=<BatteryTime.POWER_TIME_UNLIMITED: -2>, power_plugged=True)
其中percent表示電池電量剩余百分比;power_plugged為True,表示電源已連接。
到此這篇關(guān)于Python利用psutil獲取CPU與內(nèi)存等硬件信息的文章就介紹到這了,更多相關(guān)Python psutil獲取硬件信息內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python queue隊(duì)列原理與應(yīng)用案例分析
這篇文章主要介紹了Python queue隊(duì)列原理與應(yīng)用,結(jié)合具體案例形式分析了Python queue隊(duì)列的原理、功能、實(shí)現(xiàn)方法與使用技巧,需要的朋友可以參考下2019-09-09
一文帶你深入理解Flask中的Session和Cookies
Flask,作為一個(gè)靈活的微型 web 框架,提供了會(huì)話(Session)和 Cookies 管理的能力,本文將深入探討 Flask 中的會(huì)話和 Cookies 的概念、工作機(jī)制以及應(yīng)用實(shí)例,希望對大家有所幫助2023-12-12
python3 打印輸出字典中特定的某個(gè)key的方法示例
這篇文章主要介紹了python3 打印輸出字典中特定的某個(gè)key的方法,涉及Python字典的遍歷、判斷、輸出等相關(guān)操作技巧,需要的朋友可以參考下2019-07-07
Queue隊(duì)列中join()與task_done()的關(guān)系及說明
這篇文章主要介紹了Queue隊(duì)列中join()與task_done()的關(guān)系及說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
在Tensorflow中查看權(quán)重的實(shí)現(xiàn)
今天小編就為大家分享一篇在Tensorflow中查看權(quán)重的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01
Python代碼一鍵轉(zhuǎn)Jar包及Java調(diào)用Python新姿勢
這篇文章主要介紹了Python一鍵轉(zhuǎn)Jar包,Java調(diào)用Python新姿勢,本文通過截圖實(shí)例給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03

