詳解Python3.6安裝psutil模塊和功能簡(jiǎn)介
一、psutil模塊
1. psutil是一個(gè)跨平臺(tái)庫(kù),能夠輕松實(shí)現(xiàn)獲取系統(tǒng)運(yùn)行的進(jìn)程和系統(tǒng)利用率(包括CPU、內(nèi)存、磁盤、網(wǎng)絡(luò)等)信息。它主要應(yīng)用于系統(tǒng)監(jiān)控,分析和限制系統(tǒng)資源及進(jìn)程的管理。它實(shí)現(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時(shí)間比
>>> psutil.cpu_times().user 11684.4
查看cpu邏輯個(gè)數(shù)
>>> psutil.cpu_count() 1
查看cpu物理個(gè)數(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)總計(jì)內(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(磁盤讀時(shí)間),write_time(磁盤寫時(shí)間),這些IO信息用
psutil.disk_io_counters()
獲取磁盤的完整信息
psutil.disk_partitions()
獲取分區(qū)表的參數(shù)
psutil.disk_usage('/') #獲取/分區(qū)的狀態(tài)
獲取硬盤IO總個(gè)數(shù)
psutil.disk_io_counters()
獲取單個(gè)分區(qū)IO個(gè)數(shù)
psutil.disk_io_counters(perdisk=True) #perdisk=True參數(shù)獲取單個(gè)分區(qū)IO個(gè)數(shù)
讀取網(wǎng)絡(luò)信息
網(wǎng)絡(luò)信息與磁盤IO信息類似,涉及到幾個(gè)關(guān)鍵點(diǎ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ò)每個(gè)接口信息
psutil.net_io_counters(pernic=True) #pernic=True
獲取當(dāng)前系統(tǒng)用戶登錄信息
psutil.users()
獲取開機(jī)時(shí)間
psutil.boot_time() #以linux時(shí)間格式返回
datetime.datetime.fromtimestamp(psutil.boot_time ()).strftime("%Y-%m-%d %H: %M: %S") #轉(zhuǎn)換成自然時(shí)間格式
系統(tǒng)進(jìn)程管理
獲取當(dāng)前系統(tǒng)的進(jìn)程信息,獲取當(dāng)前程序的運(yùn)行狀態(tài),包括進(jìn)程的啟動(dòng)時(shí)間,查看設(shè)置CPU親和度,內(nèi)存使用率,IO信息
socket連接,線程數(shù)等
獲取進(jìn)程信息
查看系統(tǒng)全部進(jìn)程
psutil.pids()
查看單個(gè)進(jìn)程
p = psutil.Process(2423) p.name() #進(jìn)程名 p.exe() #進(jìn)程的bin路徑 p.cwd() #進(jìn)程的工作目錄絕對(duì)路徑 p.status() #進(jìn)程狀態(tài) p.create_time() #進(jìn)程創(chuàng)建時(shí)間 p.uids() #進(jìn)程uid信息 p.gids() #進(jìn)程的gid信息 p.cpu_times() #進(jìn)程的cpu時(shí)間信息,包括user,system兩個(gè)cpu信息 p.cpu_affinity() #get進(jìn)程cpu親和度,如果要設(shè)置cpu親和度,將cpu號(hào)作為參考就好 p.memory_percent() #進(jìn)程內(nèi)存利用率 p.memory_info() #進(jìn)程內(nèi)存rss,vms信息 p.io_counters() #進(jìn)程的IO信息,包括讀寫IO數(shù)字及參數(shù) p.connectios() #返回進(jìn)程列表 p.num_threads() #進(jìn)程開啟的線程數(shù) 聽過psutil的Popen方法啟動(dòng)應(yīng)用程序,可以跟蹤程序的相關(guān)信息 from subprocess import PIPE p = psutil.Popen(["/usr/bin/python", "-c", "print('hello')"],stdout=PIPE) p.name() p.username()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python抖音快手代碼舞(字符舞)的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于Python抖音快手代碼舞的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02python3安裝pip3(install pip3 for python 3.x)
這篇文章主要為大家詳細(xì)介紹了install pip3 for python 3.x,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04詳解Python小數(shù)據(jù)池和代碼塊緩存機(jī)制
這篇文章主要介紹了詳解Python 小數(shù)據(jù)池和代碼塊緩存機(jī)制的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-04-04python實(shí)現(xiàn)用于測(cè)試網(wǎng)站訪問速率的方法
這篇文章主要介紹了python實(shí)現(xiàn)用于測(cè)試網(wǎng)站訪問速率的方法,涉及Python中urllib2模塊及時(shí)間的相關(guān)操作技巧,需要的朋友可以參考下2015-05-05