[python-win32] Capturing a Python script's Task Manager statistics
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  uses it, for example, and I have had correspondence
with several people who used my wmi module  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  but often you'll just want to
watch the attributes for the Win32_Process  class (possibly specifying by
process_id). You can either poll, or you can use the WMI event model.
More information about the python-win32