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

python psutil模塊使用方法解析

系統(tǒng) 1694 0

psutil(進(jìn)程和系統(tǒng)實(shí)用程序)是一個(gè)跨平臺(tái)的庫,用于 在Python中檢索有關(guān)運(yùn)行進(jìn)程和系統(tǒng)利用率(CPU,內(nèi)存,磁盤,網(wǎng)絡(luò),傳感器)的信息。

它主要用于系統(tǒng)監(jiān)視,分析和限制流程資源以及運(yùn)行流程的管理。它實(shí)現(xiàn)了UNIX命令行工具提供的許多功能,例如:ps,top,lsof,netstat,ifconfig,who,df,kill,free,nice,ionice,iostat,iotop,uptime,pidof,tty,taskset,pmap。psutil目前支持以下平臺(tái):

  • Linux的
  • 視窗
  • OSX,
  • FreeBSD,OpenBSD,NetBSD
  • Sun Solaris
  • AIX

... 32位和64位體系結(jié)構(gòu),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 #獲取單項(xiàng)cpu的數(shù)據(jù)信息,如用戶user的cpu時(shí)間比
c = psutil.cpu_count() #獲取cpu的邏輯個(gè)數(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('/')  #獲取整個(gè)硬盤的信息
disk_usage_c = psutil.disk_usage('C://') #獲取分區(qū)c的硬盤信息
disk_io = psutil.disk_io_counters() #獲取硬盤的總io個(gè)數(shù)、讀寫信息
disk_io_perdisk = psutil.disk_io_counters(perdisk=True) #‘perdisk=True'參數(shù)獲取單個(gè)分區(qū)IO個(gè)數(shù)、讀寫信息
print (disk_partitions)
print (disk_usage)
print (disk_usage_c)
print ('硬盤總io=' +str(disk_io))
print ('單個(gè)分區(qū)信息='+str(disk_io_perdisk))

'''
網(wǎng)絡(luò)信息
'''
net_io = psutil.net_io_counters()  #獲取網(wǎng)絡(luò)總IO信息、默認(rèn)pernic=False
net_io_pernic = psutil.net_io_counters(pernic=True) #獲取每個(gè)網(wǎng)卡的io信息
net_connections = psutil.net_connections()#獲取所有的連接信息
print (net_io)
print (net_io_pernic)
print (net_connections)

'''
其他系統(tǒng)信息
'''
users = psutil.users()   #當(dāng)前登錄系統(tǒng)的用戶信息
import datetime
boot_time = psutil.boot_time() #獲取開機(jī)時(shí)間,為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)進(jìn)程管理

            
#! /env python3
#coding=utf-8
import psutil
'''
進(jìn)程信息
'''
pids = psutil.pids() #列出所有進(jìn)程id
pids_4644= psutil.Process(4644) #列出指定pid為4644的進(jìn)程信息
print (pids)
print (pids_4644.name())  #輸出進(jìn)程名
print (pids_4644.exe())   #輸出進(jìn)程路徑
print (pids_4644.cwd())   #輸出絕對(duì)路徑
print (pids_4644.status()) #輸出進(jìn)程狀態(tài)
print (pids_4644.create_time()) #輸出創(chuàng)建時(shí)間、時(shí)間戳格式
#print (pids_4644.gid())    #輸出進(jìn)程gid信息
print (pids_4644.cpu_times)   #輸出cpu時(shí)間信息,包括user,system兩個(gè)cpu時(shí)間
print (pids_4644.cpu_affinity()) #get進(jìn)程cpu親和度
print (pids_4644.memory_percent()) #進(jìn)程利用率
print (pids_4644.memory_info)  #進(jìn)程內(nèi)存信息
print (pids_4644.io_counters()) #進(jìn)程io信息,包括讀寫IO數(shù)及字節(jié)數(shù)
print (pids_4644.connections())   #返回打開進(jìn)程sockert的namedutples列表、包括fs,family等信息
print (pids_4644.num_threads())   #進(jìn)程開啟的線程數(shù)

'''
popen類的使用
'''
import psutil
from subprocess import PIPE  #通過psutil的popen方法啟動(dòng)的應(yīng)用程序,可以跟蹤該程序的所有相關(guān)信息
          

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

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

【本文對(duì)您有幫助就好】

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

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 临沧市| 济南市| 桦川县| 措美县| 新巴尔虎左旗| 德州市| 海宁市| 精河县| 措美县| 山阳县| 广宗县| 临沭县| 西贡区| 阿坝县| 嵊泗县| 定结县| 辽宁省| 贺兰县| 平潭县| 博乐市| 海兴县| 郸城县| 三穗县| 禄劝| 龙口市| 崇州市| 左权县| 鸡东县| 高青县| 呼和浩特市| 兴城市| 灵丘县| 无锡市| 汾西县| 沛县| 榆中县| 衡东县| 湖口县| 盐山县| 普格县| 抚顺市|