[Python Study Notes]psutil模組

來源:互聯網
上載者:User

標籤:登入系統   wait   進程建立   pad   win   str   登入   sha   buffer   

系統效能資訊模組psutil

psutil是一個跨平台庫,能夠輕鬆實現擷取系統啟動並執行進程和系統利用率(CPU,記憶體,磁碟,網路等)資訊,主要應用於系統監控,分析和限制系統資源及進程的管理,它實現了同等命令列工具提供的功能,如ps,top,lsof,netstat,ifconfig,who,df,kill,free,nice等.支援32位,和64位的Linux,Windows,OS X,FreeBSD等作業系統。

1 cpu資訊#Linux系統cpu利用率有以下幾個部分#User Time,執行使用者進程的時間百分比#System Time,執行核心進程和中斷的時間百分比#Wait IO,由於IO等待使cpu處於idle(空閑)狀態的時間百分比#Idle,cpu處於空閑狀態的時間百分比 >>> import psutil#擷取cpu完成資訊>>> psutil.cpu_times()scputimes(user=29.36, nice=0.0, system=26.59, idle=9619.35, iowait=64.78, irq=0.5, softirq=1.67, steal=0.0, guest=0.0) #擷取使用者user的cpu時間比>>> psutil.cpu_times().user34.54 #擷取cpu邏輯個數>>> psutil.cpu_count()4 #擷取cpu物理個數>>> psutil.cpu_count(logical=False)4 2 記憶體資訊#Linux系統的記憶體利用率資訊涉及#total 記憶體總數#used 已使用的記憶體數#free 空閑記憶體數#buffers 緩衝使用數#cache 緩衝使用數#swap 交換分區使用數 等#使用 psutil.virtual_memory()與psutil.memory()方法擷取 #擷取記憶體完整資訊>>>mem = psutil.virtual_memory()>>> memsvmem(total=1961488384, available=1483986944, percent=24.3, used=341475328, free=941473792, active=677801984, inactive=212774912, buffers=32038912, cached=646500352, shared=4116480) #擷取記憶體總數>>> mem.total1961488384 #擷取空閑記憶體數>>> mem.free941473792 #擷取SWAP分區資訊>>> psutil.swap_memory()sswap(total=2147475456, used=0, free=2147475456, percent=0.0, sin=0, sout=0) 3 磁碟資訊#磁碟利用率使用psutil.disk_usage()方法擷取#IO資訊 #read_count 讀IO數#write_count 寫IO數#read_bytes IO讀位元組數#write_bytes IO寫位元組數#read_time 磁碟讀時間#write_time 磁碟寫時間#使用psutil.disk_io_counters()擷取 #擷取磁碟完整資訊>>> psutil.disk_partitions()[sdiskpart(device=‘/dev/sda3‘, mountpoint=‘/‘, fstype=‘ext4‘, opts=‘rw‘), sdiskpart(device=‘/dev/sda1‘, mountpoint=‘/boot‘, fstype=‘ext4‘, opts=‘rw‘)] #擷取/分區使用方式>>> psutil.disk_usage(‘/‘)sdiskusage(total=18506760192, used=4438568960, free=13128093696, percent=25.3) #擷取硬碟總IO數>>> psutil.disk_io_counters() 4 網路資訊#bytes_sent 發送位元組數#bytes_recv 接收位元組數#packets_sent 發送資料包數#packest_recv 接收資料包數#使用psutil.net_io_counters()擷取>>> psutil.net_io_counters()  5 其他系統資訊#psutil模組還支援擷取使用者登入,開機時間等資訊#顯示當前登入系統的使用者資訊>>> psutil.users() #擷取開機時間>>> psutil.boot_time()


系統進程管理方法

psutil在擷取進程資訊方面也提供了很好的支援,包括使用psutil.pids()方法擷取所有PID,使用psutil.Process()方法擷取單個進程的名稱,路徑,系統資源使用率等資訊。

#列出所有進程的PID>>> psutil.pids() #執行個體化一個Process對象,參數為一個PID>>>p = psutil.Process(3197) #進程名>>> p.name()‘python3‘ #進程bin路徑>>> p.exe()‘/opt/python3/bin/python3.5‘ #進程工作絕對路徑>>> p.cwd() #進程狀態>>> p.status() #進程建立時間>>> p.create_time() #uid資訊>>> p.uids() #gid資訊>>> p.gids() #進程cpu時間,包括user,system兩個時間>>> p.cpu_times() #進程記憶體利用率>>> p.memory_percent() #進程記憶體rss,vms資訊>>> p.memory_info() #進程IO資訊>>> p.io_counters() #進程開啟的線程數>>> p.num_threads()


[Python Study Notes]psutil模組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.