psutil - A cross-platform process and system utilities module for Python算法
pip 安装便可。chrome
windows 下须要安装 vs2008,不然报错: Unable to find vcvarsall.bat windows
若是已经安装 vs2010 / vs2012 则须要设置环境变量,VS90COMNTOOLS 指向已有的 vs 变量。less
vs2010 设置以下:ide
VS90COMNTOOLS = %VS100COMNTOOLS%
# -*- coding: utf-8-*- import psutil def get_proc_by_id(pid): return psutil.Process(pid) def get_proc_by_name(pname): """ get process by name return the first process if there are more than one """ for proc in psutil.process_iter(): try: if proc.name().lower() == pname.lower(): return proc # return if found one except psutil.AccessDenied: pass except psutil.NoSuchProcess: pass return None if '__main__' == __name__: print get_proc_by_name("chrome.exe") print get_proc_by_id(4364)
不一样的进程,权限等信息可能不一样,遍历全部进程取信息时,须要对每个进程单独进程异常保护。this
大多数 demo 代码中,都是使用 psutil.get_process_list ,但该方法在源码中已经标记为废弃。spa
新推荐的是 psutil.process_iter 迭代器。code
根据下面的源码可知实现原理:获取全部进程 ID,而后根据 ID 建立进程对象。orm
_pmap = {} def process_iter(): """Return a generator yielding a Process class instance for all running processes on the local machine. Every new Process instance is only created once and then cached into an internal table which is updated every time this is used. Cached Process instances are checked for identity so that you're safe in case a PID has been reused by another process, in which case the cached instance is updated. The sorting order in which processes are yielded is based on their PIDs. """ def add(pid): proc = Process(pid) _pmap[proc.pid] = proc return proc def remove(pid): _pmap.pop(pid, None) a = set(get_pid_list()) b = set(_pmap.keys()) new_pids = a - b gone_pids = b - a for pid in gone_pids: remove(pid) for pid, proc in sorted(list(_pmap.items()) + \ list(dict.fromkeys(new_pids).items())): try: if proc is None: # new process yield add(pid) else: # use is_running() to check whether PID has been reused by # another process in which case yield a new Process instance if proc.is_running(): yield proc else: yield add(pid) except NoSuchProcess: remove(pid) except AccessDenied: # Process creation time can't be determined hence there's # no way to tell whether the pid of the cached process # has been reused. Just return the cached version. yield proc @_deprecated() def get_process_list(): """Return a list of Process class instances for all running processes on the local machine (deprecated). """ return list(process_iter())
VSS 是剩余的可访问内存。对象
进程占用内存包括 2 部分,自身 + 共享库。不一样的算法产生了 3 个不一样的内存指标,分别是:RSS / PSS / USS。
通常来讲内存占用大小有以下规律:VSS >= RSS >= PSS >= USS
Demo 代码以下
proc = psutil.Process(4364) total = psutil.virtual_memory().total rss, vss = proc.memory_info() percent = proc.memory_percent() print "rss: %s Byte, vss: %s Byte" % (rss, vss) print "total: %.2f(M)" % (float(total)/1024/1024/1024) print "percent: %.2f%%, calc: %.2f%%" % (percent, 100*float(rss)/total)
输出
本机内存信息截图
详细说明: