溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在python中調(diào)用psutil模塊

發(fā)布時(shí)間:2021-03-10 16:05:33 來源:億速云 閱讀:197 作者:Leah 欄目:開發(fā)技術(shù)

這篇文章給大家介紹怎么在python中調(diào)用psutil模塊,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

oshelper.py

#encoding=utf-8
import psutil 
import datetime 
 
#查看cpu的信息 
print u"CPU 個(gè)數(shù) %s"%psutil.cpu_count() 
print u"物理CPU個(gè)數(shù) %s"%psutil.cpu_count(logical=False) 
print u"CPU uptimes" 
print psutil.cpu_times() 
print "" 
 
#查看內(nèi)存信息 
mem = psutil.virtual_memory()
print u"系統(tǒng)總內(nèi)存 %s G"%(mem.total/1024/1024/1024) 
print u"系統(tǒng)可用內(nèi)存 %s G"%(mem.available/1024/1024/1024) 
mem_rate = int(mem.available)/float(mem.total) 
print u"系統(tǒng)內(nèi)存使用率 %s %%"%int(mem_rate*100) 

#交換分區(qū)
swapmem = psutil.swap_memory()
print u"交換分區(qū) %s G"%(swapmem.total/1024/1024/1024) 
print u"交換分區(qū)可用 %s G"%(swapmem.free/1024/1024/1024) 
print u"交換分區(qū)使用率 %s %%"%int(swapmem.percent) 
#系統(tǒng)啟動(dòng)時(shí)間 
print u"系統(tǒng)啟動(dòng)時(shí)間 %s"%datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S") 
 
#系統(tǒng)用戶 
users_count = len(psutil.users()) 
users_list = ",".join([ u.name for u in psutil.users()]) 
print u"當(dāng)前有%s個(gè)用戶,分別是%s"%(users_count, users_list) 
 
#網(wǎng)卡,可以得到網(wǎng)卡屬性,連接數(shù),當(dāng)前流量等信息 
net = psutil.net_io_counters() 
bytes_sent = '{0:.2f} Mb'.format(net.bytes_recv / 1024 / 1024) 
bytes_rcvd = '{0:.2f} Mb'.format(net.bytes_sent / 1024 / 1024) 
print u"網(wǎng)卡接收流量 %s 網(wǎng)卡發(fā)送流量 %s"%(bytes_rcvd, bytes_sent) 
nis=psutil.net_io_counters(pernic=True)
print u"網(wǎng)卡 " ,tuple(nis)

#進(jìn)程 進(jìn)程的各種詳細(xì)參數(shù) 
def show_process(pid):
  try:
    p = psutil.Process(pid) 
  
    p.name()  #進(jìn)程名
    #p.exe()  #進(jìn)程的bin路徑
    #p.cwd()  #進(jìn)程的工作目錄絕對路徑
    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()
  except:
    pass
'''
pids=psutil.pids()
for pid in pids:
  show_process(pid)
'''
with open (r'd:\temp\test.txt','w') as f:
  sys.stdout=f
  for proc in psutil.process_iter():
  '''
    if proc.name() == "w3wp.exe":
      cpu_threshold=proc.cpu_percent(interval=2)/24 
      print proc.name(),proc.create_time(),cpu_threshold
      p.terminate() proc .kill()
      Iterate over all ports this process is listening to 
    for con in proc.get_connections():
      con
  '''
    pa=proc.as_dict()
    print pa.get('name'),pa.get('create_time'),pa.get('pid'),pa.get('status'),pa.get('connections'),pa.get('open_files'),pa.get('cpu_percent'),pa.get('memory_percent'),pa.get('username'),pa.get('num_threads')

print u"當(dāng)前進(jìn)程:",psutil.Process(os.getpid()).cmdline()

#磁盤 磁盤的使用量等等 
dps=psutil.disk_partitions()
for dp in dps:
  dp
du=psutil.disk_usage('/')
print u"硬盤總共容量 %s G"%(du.total/1024/1024/1024) 
print u"硬盤可用 %s G"%(du.free/1024/1024/1024)
print u"硬盤已用 %s %%"%(du.percent)

關(guān)于怎么在python中調(diào)用psutil模塊就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI