[python-win32] Capturing a Python script's Task Manager statistics

Tim Golden mail at timgolden.me.uk
Fri Jun 20 17:33:05 CEST 2008

python at bdurham.com wrote:
> Anyone know of a way to have a Python script capture its own Task
> Manager statistics?
> I have a long running Python script whose CPU, memory, disk i/o, and
> network traffic I would like to monitor.
> An alternative more generic approach would be to run an 'observer'
> script that would monitor all Task Manager statistics so I could watch
> multiple scripts.
> Motivation: I would like to track this script's resource requirements
> and if the script detects its exceeding some pre-defined resource limits
> for a certain amount of time, to send out an alert.

This is the kind of thing which WMI was probably designed for.
Zenoss [1] uses it, for example, and I have had correspondence
with several people who used my wmi module [2] for this sort of

One caveat: WMI is basically a uniform layer over the more complex
API. It's *not* the fastest thing on earth and less so when filtered through
a Python wrapper round a Dispatch-based wrapper round the WMI
COM interface. YMMV.

There *are* specific performance classes [3] but often you'll just want to 
watch the attributes for the Win32_Process [4] class (possibly specifying by 
process_id). You can either poll, or you can use the WMI event model.[5][6]


[1] http://www.zenoss.com/
[2] http://timgolden.me.uk/python/wmi.html
[3] http://msdn.microsoft.com/en-us/library/aa392397(VS.85).aspx
[4] http://msdn.microsoft.com/en-us/library/aa394372(VS.85).aspx
[5] http://msdn.microsoft.com/en-us/library/aa393013(VS.85).aspx
[6] http://tgolden.sc.sabren.com/python/wmi-tutorial.html#monitoring

More information about the python-win32 mailing list