日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

python psutil模塊使用方法解析

系統 1760 0

psutil(進程和系統實用程序)是一個跨平臺的庫,用于 在Python中檢索有關運行進程和系統利用率(CPU,內存,磁盤,網絡,傳感器)的信息。

它主要用于系統監視,分析和限制流程資源以及運行流程的管理。它實現了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、獲取系統性能信息

            
#! /env python3
#coding=utf-8
import psutil
''''
獲取cpu信息
'''
a = psutil.cpu_times() #使用cpu_times方法獲取cpu完成信息,需要顯示所有的cpu信息
b = psutil.cpu_times().user #獲取單項cpu的數據信息,如用戶user的cpu時間比
c = psutil.cpu_count() #獲取cpu的邏輯個數

print (a)
print (b)
print (c)

'''
內存信息
'''
mem = psutil.virtual_memory()  #使用pstuil.virtual_memory方法獲取內存的完整信息
mem_total = psutil.virtual_memory().total #獲取內存總數
mem_free = psutil.virtual_memory().free #獲取內存剩余
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://') #獲取分區c的硬盤信息
disk_io = psutil.disk_io_counters() #獲取硬盤的總io個數、讀寫信息
disk_io_perdisk = psutil.disk_io_counters(perdisk=True) #‘perdisk=True'參數獲取單個分區IO個數、讀寫信息
print (disk_partitions)
print (disk_usage)
print (disk_usage_c)
print ('硬盤總io=' +str(disk_io))
print ('單個分區信息='+str(disk_io_perdisk))

'''
網絡信息
'''
net_io = psutil.net_io_counters()  #獲取網絡總IO信息、默認pernic=False
net_io_pernic = psutil.net_io_counters(pernic=True) #獲取每個網卡的io信息
net_connections = psutil.net_connections()#獲取所有的連接信息
print (net_io)
print (net_io_pernic)
print (net_connections)

'''
其他系統信息
'''
users = psutil.users()   #當前登錄系統的用戶信息
import datetime
boot_time = psutil.boot_time() #獲取開機時間,為linux格式
boot_time_nu = datetime.datetime.fromtimestamp(psutil.boot_time()).strftime('%Y-%m-%d%H:%M:%S') #轉換為自然格式
print (users)
print (boot_time)
print (boot_time_nu)
          

2、系統進程管理

            
#! /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()) #輸出進程狀態
print (pids_4644.create_time()) #輸出創建時間、時間戳格式
#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)  #進程內存信息
print (pids_4644.io_counters()) #進程io信息,包括讀寫IO數及字節數
print (pids_4644.connections())   #返回打開進程sockert的namedutples列表、包括fs,family等信息
print (pids_4644.num_threads())   #進程開啟的線程數

'''
popen類的使用
'''
import psutil
from subprocess import PIPE  #通過psutil的popen方法啟動的應用程序,可以跟蹤該程序的所有相關信息
          

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 滦南县| 江北区| 苗栗县| 沙田区| 江津市| 旬阳县| 疏勒县| 伽师县| 安阳县| 拜泉县| 安西县| 娱乐| 丹棱县| 吉首市| 河津市| 荔浦县| 高碑店市| 陆丰市| 沂南县| 西乡县| 三门县| 巧家县| 芮城县| 苏尼特右旗| 石棉县| 吉木萨尔县| 榆中县| 科尔| 贡觉县| 太白县| 墨竹工卡县| 扎鲁特旗| 凤阳县| 拜泉县| 延边| 常德市| 朝阳市| 布拖县| 瑞昌市| 嘉义县| 蕲春县|