python在linux系统下获取系统内存使用情况,pythonlinux,"""Simple mo
python在linux系统下获取系统内存使用情况,pythonlinux,"""Simple mo
"""Simple module for getting amount of memory used by a specified user'sprocesses on a UNIX system.It uses UNIX ps utility to get the memory usage for a specified username andpipe 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 subprocessclass 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
调用方法如下:
from memorymonitor import MemoryMonitormemory_mon = MemoryMonitor('username')used_memory = memory_mon.usage()
相关内容
- 移除html事件,移除事件,#!/usr/bin/e
- 三连棋(python 3.2),连棋python3.2,python 3.2学p
- Inverse power method for solving the eigenvalue problem in Python,i
- python拆分中英文混合字符串 - 按中文拆分,python中英文
- python语言解决《Ruby太慢了》问题,,python 2.6ec
- Python 处理图像几何变换,python图像几何变换,import Ima
- python递归解决0-1背包问题,python递归0-1背包,#coding:utf
- 检查小米手机是否可以购买,检查小米手机购买,饥饿营
- 多线程,限制线程数运行,,多线程限制线程数,#encoding
- 判断图片分辨率并修改,判断图片分辨率,碰到许多图片
评论关闭