詳解Python3.6安裝psutil模塊和功能簡介
一、psutil模塊
1. psutil是一個跨平臺庫,能夠輕松實現(xiàn)獲取系統(tǒng)運行的進程和系統(tǒng)利用率(包括CPU、內(nèi)存、磁盤、網(wǎng)絡(luò)等)信息。它主要應(yīng)用于系統(tǒng)監(jiān)控,分析和限制系統(tǒng)資源及進程的管理。它實現(xiàn)了同等命令行工具提供的功能,如ps、top、lsof、netstat、ifconfig、who、df、kill、free、nice、ionice、iostat、iotop、uptime、pidof、tty、taskset、pmap等。目前支持32位和64位的Linux、Windows、OS X、FreeBSD和Sun Solaris等操作系統(tǒng).
psutil下載地址(官網(wǎng)):https://pypi.python.org/pypi/psutil/
psutil下載地址(github): https://github.com/giampaolo/psutil/
2、window10操作系統(tǒng)(Python 3.6開發(fā)環(huán)境)安裝psutil
D:\Program Files\python\Scripts>pip.exe install D:\python\psutil-5.2.2-cp36-cp36m-win_amd64.whl Processing d:\python\psutil-5.2.2-cp36-cp36m-win_amd64.whl Installing collected packages: psutil Successfully installed psutil-5.2.2
psutil.whl文件待上傳:
3.使用
獲取系統(tǒng)性能信息(CPU,內(nèi)存,磁盤,網(wǎng)絡(luò))
3.1CPU相關(guān)
查看cpu信息
import Psutil #查看cpu所有信息 >>> psutil.cpu_times() scputimes(user=11677.09, nice=57.93, system=148675.58, idle=2167147.79, iowait=260828.48, irq=7876.28, softirq=0.0, steal=3694.59, guest=0.0, guest_nice=0.0)
顯示cpu所有邏輯信息
>>> psutil.cpu_times(percpu=True) [scputimes(user=11684.17, nice=57.93, system=148683.01, idle=2168982.08, iowait=260833.18, irq=7882.35, softirq=0.0, steal=3697.3, guest=0.0, guest_nice=0.0)]
查看用戶的cpu時間比
>>> psutil.cpu_times().user 11684.4
查看cpu邏輯個數(shù)
>>> psutil.cpu_count() 1
查看cpu物理個數(shù)
>>> psutil.cpu_count(logical=False) 1
3.2查看系統(tǒng)內(nèi)存
>>> import psutil >>> mem = psutil.virtual_memory() >>> mem #系統(tǒng)內(nèi)存的所有信息 svmem(total=1040662528, available=175054848, percent=83.2, used=965718016, free=74944512, active=566755328, inactive=59457536, buffers=9342976, cached=90767360)
系統(tǒng)總計內(nèi)存
>>> mem.total 1040662528
系統(tǒng)已經(jīng)使用內(nèi)存
>>> mem.used 965718016
系統(tǒng)空閑內(nèi)存
>>> mem.free 112779264
獲取swap內(nèi)存信息
>>> psutil.swap_memory() sswap(total=0, used=0, free=0, percent=0, sin=0, sout=0)
讀取磁盤參數(shù)
磁盤利用率使用psutil.disk_usage方法獲取,
磁盤IO信息包括read_count(讀IO數(shù)),write_count(寫IO數(shù))
read_bytes(IO寫字節(jié)數(shù)),read_time(磁盤讀時間),write_time(磁盤寫時間),這些IO信息用
psutil.disk_io_counters()
獲取磁盤的完整信息
psutil.disk_partitions()
獲取分區(qū)表的參數(shù)
psutil.disk_usage('/') #獲取/分區(qū)的狀態(tài)
獲取硬盤IO總個數(shù)
psutil.disk_io_counters()
獲取單個分區(qū)IO個數(shù)
psutil.disk_io_counters(perdisk=True) #perdisk=True參數(shù)獲取單個分區(qū)IO個數(shù)
讀取網(wǎng)絡(luò)信息
網(wǎng)絡(luò)信息與磁盤IO信息類似,涉及到幾個關(guān)鍵點,包括byes_sent(發(fā)送字節(jié)數(shù)),byte_recv=xxx(接受字節(jié)數(shù)),
pack-ets_sent=xxx(發(fā)送字節(jié)數(shù)),pack-ets_recv=xxx(接收數(shù)據(jù)包數(shù)),這些網(wǎng)絡(luò)信息用
獲取網(wǎng)絡(luò)總IO信息
psutil.net_io_counters()
輸出網(wǎng)絡(luò)每個接口信息
psutil.net_io_counters(pernic=True) #pernic=True
獲取當(dāng)前系統(tǒng)用戶登錄信息
psutil.users()
獲取開機時間
psutil.boot_time() #以linux時間格式返回
datetime.datetime.fromtimestamp(psutil.boot_time ()).strftime("%Y-%m-%d %H: %M: %S") #轉(zhuǎn)換成自然時間格式
系統(tǒng)進程管理
獲取當(dāng)前系統(tǒng)的進程信息,獲取當(dāng)前程序的運行狀態(tài),包括進程的啟動時間,查看設(shè)置CPU親和度,內(nèi)存使用率,IO信息
socket連接,線程數(shù)等
獲取進程信息
查看系統(tǒng)全部進程
psutil.pids()
查看單個進程
p = psutil.Process(2423) p.name() #進程名 p.exe() #進程的bin路徑 p.cwd() #進程的工作目錄絕對路徑 p.status() #進程狀態(tài) p.create_time() #進程創(chuàng)建時間 p.uids() #進程uid信息 p.gids() #進程的gid信息 p.cpu_times() #進程的cpu時間信息,包括user,system兩個cpu信息 p.cpu_affinity() #get進程cpu親和度,如果要設(shè)置cpu親和度,將cpu號作為參考就好 p.memory_percent() #進程內(nèi)存利用率 p.memory_info() #進程內(nèi)存rss,vms信息 p.io_counters() #進程的IO信息,包括讀寫IO數(shù)字及參數(shù) p.connectios() #返回進程列表 p.num_threads() #進程開啟的線程數(shù) 聽過psutil的Popen方法啟動應(yīng)用程序,可以跟蹤程序的相關(guān)信息 from subprocess import PIPE p = psutil.Popen(["/usr/bin/python", "-c", "print('hello')"],stdout=PIPE) p.name() p.username()
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python3安裝pip3(install pip3 for python 3.x)
這篇文章主要為大家詳細介紹了install pip3 for python 3.x,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04python實現(xiàn)用于測試網(wǎng)站訪問速率的方法
這篇文章主要介紹了python實現(xiàn)用于測試網(wǎng)站訪問速率的方法,涉及Python中urllib2模塊及時間的相關(guān)操作技巧,需要的朋友可以參考下2015-05-05