[Python] python在linux系统下获取系统内存使用情况 →→→→→进入此内容的聊天室

来自 , 2020-01-28, 写在 Python, 查看 112 次.
URL http://www.code666.cn/view/f7426bc1
  1. """Simple module for getting amount of memory used by a specified user's
  2. processes on a UNIX system.
  3. It uses UNIX ps utility to get the memory usage for a specified username and
  4. pipe it to awk for summing up per application memory usage and return the total.
  5. Python's Popen() from subprocess module is used for spawning ps and awk.
  6.  
  7. """
  8.  
  9. import subprocess
  10.  
  11. class MemoryMonitor(object):
  12.  
  13.     def __init__(self, username):
  14.         """Create new MemoryMonitor instance."""
  15.         self.username = username
  16.  
  17.     def usage(self):
  18.         """Return int containing memory used by user's processes."""
  19.         self.process = subprocess.Popen("ps -u %s -o rss | awk '{sum+=$1} END {print sum}'" % self.username,
  20.                                         shell=True,
  21.                                         stdout=subprocess.PIPE,
  22.                                         )
  23.         self.stdout_list = self.process.communicate()[0].split('\n')
  24.         return int(self.stdout_list[0])
  25.  
  26.  
  27. #//python/7362

回复 "python在linux系统下获取系统内存使用情况"

这儿你可以回复上面这条便签

captcha