十年網(wǎng)站開(kāi)發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專業(yè)推廣+無(wú)憂售后,網(wǎng)站問(wèn)題一站解決
這篇文章給大家介紹怎么在python中調(diào)用psutil模塊,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
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)程的工作目錄絕對(duì)路徑 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信息,包括讀寫(xiě)IO數(shù)字及參數(shù) #p.connectios() #返回進(jìn)程列表 p.num_threads() #進(jìn)程開(kāi)啟的線程數(shù) ''' 聽(tīng)過(guò)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() #磁盤(pán) 磁盤(pán)的使用量等等 dps=psutil.disk_partitions() for dp in dps: dp du=psutil.disk_usage('/') print u"硬盤(pán)總共容量 %s G"%(du.total/1024/1024/1024) print u"硬盤(pán)可用 %s G"%(du.free/1024/1024/1024) print u"硬盤(pán)已用 %s %%"%(du.percent)
關(guān)于怎么在python中調(diào)用psutil模塊就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。