python 得到当前进程的信息(cpu 内存占用等)

可以用这个函数:

import commands, os, re

def process_info():
    pid = os.getpid()
    res = commands.getstatusoutput('ps aux|grep '+str(pid))[1].split('\n')[0]

    p = re.compile(r'\s+')
    l = p.split(res)
    info = {'user':l[0],
        'pid':l[1],
        'cpu':l[2],
        'mem':l[3],
        'vsa':l[4],
        'rss':l[5],
        'start_time':l[6]}
    return info
 

猜你喜欢

转载自songpengfei.iteye.com/blog/1701654