Problems with win32pdh

Mark Hammond mhammond at skippinet.com.au
Fri Jun 13 12:33:33 CEST 2003


Eric Jensen wrote:

> I am trying to get the cpu utilization on a XP box.My code looks like this:
> 
> The problem is that this always returns 99 % utilization

That is because your CPU is busy executing this code :)

Try this:

import win32pdh, time
my_query = win32pdh.OpenQuery( None, 0 )
my_counter = win32pdh.AddCounter( my_query, "\\Processor(_Total)\\% 
Processor Time", 0 )
for i in range(20):
     win32pdh.CollectQueryData( my_query )
     junk, percent = win32pdh.GetFormattedCounterValue( my_counter,
     win32pdh.PDH_FMT_LONG )
     print "Junk:", junk
     print "Percent: " + str( percent ) + "%"
     time.sleep(0.1)
win32pdh.CloseQuery( my_query )





More information about the Python-list mailing list