获得处理器和系统相关信息的模块python psutil

python psutil:一个用于获得处理器和系统相关信息的模块,利用它,我们可以在方便得获取CPU、内存、网络流量、磁盘分区、用户等信息,相关内容可以参考:
http://pydoc.net/Python/psutil/0.4.1/

psutil能干的事是:提供了个接口,可以用来获取信息,包括:
   当前运行的进程
   系统(资源使用)信息
   CPU
   内存
   磁盘
   网络
   用户

例子分析:
http://code.google.com/p/psutil/

python 使用psutil根据进程名获取PID:

import psutil
import re
import sys

def processinfo(x):
   p = psutil.get_process_list()
   for r in p:
   aa = str(r)
   f = re.compile(x,re.I)
   if f.search(aa):
   #        print aa.split('pid=')[1].split(',')[0]  
   print aa.split('pid=')
processinfo(sys.argv[1])

psutil is a module providing an interface for retrieving information on all running processes and system utilization (CPU, memory, disks, network, users) in a portable way by using Python

实验环境:ubuntu12.10

psutil版本:0.6.1

包含类:
class AccessDenied(Error)
class Error(exceptions.Exception)
class NoSuchProcess(Error)
class Popen(Process)
class Process(builtin.object)
class TimeoutExpired(Error)

方法:
cpu_percent(interval=0.1, percpu=False)
返回一个浮点数,表示整个系统CPU的利用率,百分比。

When interval is > 0.0 compares system CPU times elapsed before
and after the interval (blocking).

When interval is 0.0 or None compares system CPU times elapsed
since last call or module import, returning immediately.
In this case is recommended for accuracy that this function be
called with at least 0.1 seconds between calls.

如果percpu为True,则返回每个cpu的利用率,百分比。

cpu_times(percpu=False)
返回系统cpu的运行时间,每个cpu的时间代表在给定的模式下每个cpu运行的时间
disk_io_counters(perdisk=False)
返回系统磁盘的I/O统计数据,如果为True,则返回每一个物理磁盘的统计数据。
get_pid_list()
返回一个PID的列表
get_process_list(*args, **kwargs)
返回一个所有运行的进程类的列表
network_io_counters(pernic=False)
返回network的统计信息
pid_exists(pid)
检测一个pid是否存在
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.

The sorting order in which processes are yielded is based on
their PIDs.

swap_memory()
返回系统的swap统计信息
test()
效果如同ps aux
virtual_memory()
返回系统关于内存使用的统计信息