python在linux系統(tǒng)下獲取系統(tǒng)內(nèi)存使用情況的方法
本文實(shí)例講述了python在linux系統(tǒng)下獲取系統(tǒng)內(nèi)存使用情況的方法。分享給大家供大家參考。具體如下:
""" Simple module for getting amount of memory used by a specified user's processes on a UNIX system. It uses UNIX ps utility to get the memory usage for a specified username and pipe it to awk for summing up per application memory usage and return the total. Python's Popen() from subprocess module is used for spawning ps and awk. """ import subprocess class MemoryMonitor(object): def __init__(self, username): """Create new MemoryMonitor instance.""" self.username = username def usage(self): """Return int containing memory used by user's processes.""" self.process = subprocess.Popen("ps -u %s -o rss | awk '{sum+=$1} END {print sum}'" % self.username, shell=True, stdout=subprocess.PIPE, ) self.stdout_list = self.process.communicate()[0].split('\n') return int(self.stdout_list[0])
將上面的代碼保存為:memorymonitor.py
調(diào)用方法如下:
from memorymonitor import MemoryMonitor memory_mon = MemoryMonitor('username') used_memory = memory_mon.usage()
希望本文所述對大家的Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
python入門語句基礎(chǔ)之if語句、while語句
本文介紹了python入門語句基礎(chǔ)之if語句、while語句,if?語句讓你能夠檢查程序的當(dāng)前狀態(tài),并據(jù)此采取相應(yīng)的措施,而for?循環(huán)用于針對集合中的每個(gè)元素都一個(gè)代碼塊,而?while?循環(huán)不斷地運(yùn)行,直到指定的條件不滿足為止,本文通過示例代碼詳解介紹,需要的朋友參考下吧2022-04-04解決pytorch DataLoader num_workers出現(xiàn)的問題
今天小編就為大家分享一篇解決pytorch DataLoader num_workers出現(xiàn)的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01python manim實(shí)現(xiàn)排序算法動(dòng)畫示例
這篇文章主要為大家介紹了python manim實(shí)現(xiàn)排序算法動(dòng)畫示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08pyqt5實(shí)現(xiàn)繪制ui,列表窗口,滾動(dòng)窗口顯示圖片的方法
今天小編就為大家分享一篇pyqt5實(shí)現(xiàn)繪制ui,列表窗口,滾動(dòng)窗口顯示圖片的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06深入剖析Python的爬蟲框架Scrapy的結(jié)構(gòu)與運(yùn)作流程
這篇文章主要介紹了Python的爬蟲框架Scrapy的結(jié)構(gòu)與運(yùn)作流程,并以一個(gè)實(shí)際的項(xiàng)目來講解Scrapy的原理機(jī)制,十分推薦!需要的朋友可以參考下2016-01-01詳解Python常用標(biāo)準(zhǔn)庫之時(shí)間模塊time和datetime
time和datetime是Python中常用的兩個(gè)時(shí)間模塊,本文將通過示例詳細(xì)為大家講講二者的使用方法,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)學(xué)習(xí)2022-05-05