os.times() for other process

Colin Brown cbrown at metservice.com
Tue Jan 14 15:07:42 EST 2003


> With os.times() I get user and system time for the current process. How
can
> I get this for another process with known pid?

Hi Christian

A method I have used in the past (although not elegant, it is easy) is to
use "pslist" from the
pstools kit (www.sysinternals.com). For example:

>>> import os
>>> print os.popen(r'c:\pstools\pslist python','r').read()

PsList v1.2 - Process Information Lister
Copyright (C) 1999-2002 Mark Russinovich
Sysinternals - www.sysinternals.com

Process information for CBROWN:

Name         Pid Pri Thd  Hnd    Mem     User Time   Kernel Time   Elapsed
Time
python       904   8   2  117   4872   0:00:00.328   0:00:03.906
115:09:51.554
python      1632   8   1   26   2124   0:00:00.046   0:00:00.203
0:01:56.747

>>>

Note that pslist will also accept the pid so you would use something like:

import os
def ProcTimes(pid):
    text = os.popen(r'c:\pstools\pslist '+str(pid),'r').read()
    info = text.split('\n')[-2]
    items = info.split()
    return {'User': items[-3], 'Kernel': items[-2], 'Elapsed': items[-1]}

I hope this helps.

Colin Brown
PyNZ








More information about the Python-list mailing list