python psutil模塊使用方法解析
psutil(進程和系統(tǒng)實用程序)是一個跨平臺的庫,用于 在Python中檢索有關運行進程和系統(tǒng)利用率(CPU,內(nèi)存,磁盤,網(wǎng)絡,傳感器)的信息。
它主要用于系統(tǒng)監(jiān)視,分析和限制流程資源以及運行流程的管理。它實現(xiàn)了UNIX命令行工具提供的許多功能,例如:ps,top,lsof,netstat,ifconfig,who,df,kill,free,nice,ionice,iostat,iotop,uptime,pidof,tty,taskset,pmap。psutil目前支持以下平臺:
- Linux的
- 視窗
- OSX,
- FreeBSD,OpenBSD,NetBSD
- Sun Solaris
- AIX
... 32位和64位體系結構,Python版本從2.6到3.6。
1、獲取系統(tǒng)性能信息
#! /env python3
#coding=utf-8
import psutil
''''
獲取cpu信息
'''
a = psutil.cpu_times() #使用cpu_times方法獲取cpu完成信息,需要顯示所有的cpu信息
b = psutil.cpu_times().user #獲取單項cpu的數(shù)據(jù)信息,如用戶user的cpu時間比
c = psutil.cpu_count() #獲取cpu的邏輯個數(shù)
print (a)
print (b)
print (c)
'''
內(nèi)存信息
'''
mem = psutil.virtual_memory() #使用pstuil.virtual_memory方法獲取內(nèi)存的完整信息
mem_total = psutil.virtual_memory().total #獲取內(nèi)存總數(shù)
mem_free = psutil.virtual_memory().free #獲取內(nèi)存剩余
print (mem)
print (mem_total)
print (mem_free)
'''
磁盤信息
'''
disk_partitions = psutil.disk_partitions() #獲取磁盤完整信息
disk_usage = psutil.disk_usage('/') #獲取整個硬盤的信息
disk_usage_c = psutil.disk_usage('C://') #獲取分區(qū)c的硬盤信息
disk_io = psutil.disk_io_counters() #獲取硬盤的總io個數(shù)、讀寫信息
disk_io_perdisk = psutil.disk_io_counters(perdisk=True) #‘perdisk=True'參數(shù)獲取單個分區(qū)IO個數(shù)、讀寫信息
print (disk_partitions)
print (disk_usage)
print (disk_usage_c)
print ('硬盤總io=' +str(disk_io))
print ('單個分區(qū)信息='+str(disk_io_perdisk))
'''
網(wǎng)絡信息
'''
net_io = psutil.net_io_counters() #獲取網(wǎng)絡總IO信息、默認pernic=False
net_io_pernic = psutil.net_io_counters(pernic=True) #獲取每個網(wǎng)卡的io信息
net_connections = psutil.net_connections()#獲取所有的連接信息
print (net_io)
print (net_io_pernic)
print (net_connections)
'''
其他系統(tǒng)信息
'''
users = psutil.users() #當前登錄系統(tǒng)的用戶信息
import datetime
boot_time = psutil.boot_time() #獲取開機時間,為linux格式
boot_time_nu = datetime.datetime.fromtimestamp(psutil.boot_time()).strftime('%Y-%m-%d%H:%M:%S') #轉(zhuǎn)換為自然格式
print (users)
print (boot_time)
print (boot_time_nu)
2、系統(tǒng)進程管理
#! /env python3 #coding=utf-8 import psutil ''' 進程信息 ''' pids = psutil.pids() #列出所有進程id pids_4644= psutil.Process(4644) #列出指定pid為4644的進程信息 print (pids) print (pids_4644.name()) #輸出進程名 print (pids_4644.exe()) #輸出進程路徑 print (pids_4644.cwd()) #輸出絕對路徑 print (pids_4644.status()) #輸出進程狀態(tài) print (pids_4644.create_time()) #輸出創(chuàng)建時間、時間戳格式 #print (pids_4644.gid()) #輸出進程gid信息 print (pids_4644.cpu_times) #輸出cpu時間信息,包括user,system兩個cpu時間 print (pids_4644.cpu_affinity()) #get進程cpu親和度 print (pids_4644.memory_percent()) #進程利用率 print (pids_4644.memory_info) #進程內(nèi)存信息 print (pids_4644.io_counters()) #進程io信息,包括讀寫IO數(shù)及字節(jié)數(shù) print (pids_4644.connections()) #返回打開進程sockert的namedutples列表、包括fs,family等信息 print (pids_4644.num_threads()) #進程開啟的線程數(shù) ''' popen類的使用 ''' import psutil from subprocess import PIPE #通過psutil的popen方法啟動的應用程序,可以跟蹤該程序的所有相關信息
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
在服務器上安裝python3.8.2環(huán)境的教程詳解
這篇文章主要介紹了在服務器上安裝python3.8.2環(huán)境的教程詳解,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04
python pandas中DataFrame類型數(shù)據(jù)操作函數(shù)的方法
下面小編就為大家分享一篇python pandas中DataFrame類型數(shù)據(jù)操作函數(shù)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04
Python + opencv對拍照得到的圖片進行背景去除的實現(xiàn)方法
這篇文章主要介紹了Python + opencv對拍照得到的圖片進行背景去除的實現(xiàn)方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11
python中import,from……import的使用詳解
這篇文章主要介紹了python中import,from……import的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02
解決Pytorch自定義層出現(xiàn)多Variable共享內(nèi)存錯誤問題
這篇文章主要介紹了解決Pytorch自定義層出現(xiàn)多Variable共享內(nèi)存錯誤問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06
Python解析命令行讀取參數(shù)--argparse模塊使用方法
這篇文章主要介紹了Python解析命令行讀取參數(shù)--argparse模塊使用方法,需要的朋友可以參考下2018-01-01
PyQt+socket實現(xiàn)遠程操作服務器的方法示例
這篇文章主要介紹了PyQt+socket實現(xiàn)遠程操作服務器的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-08-08

