Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > RE: Get CPU usage of a single process in Windows

Reply
Thread Tools

RE: Get CPU usage of a single process in Windows

 
 
Tim Golden
Guest
Posts: n/a
 
      09-12-2006
[Gerrit Muller]

| If you have a working example of CPU usage could you post the
| result? I would be interested.

OK. Here's a workingish example, cut down from the link
I posted earlier. This one was designed to work with Win2K
which I was using at the time. For WinXP and later, there's
a new counter with the ungainly name of

Win32_PerfFormattedData_PerfProc_Process

which should give you the number straight off without having
to do the take-and-diff-and-divide dance. However, it doesn't
seem to do anything useful on my (XP) system. Haven't tried
that hard, I admit.

As ever, if you can find any example around the Web -- and there
are loads -- converting it to Python should be a breeze.

TJG

<code>
import time
import wmi

c = wmi.WMI ()

process_info = {}
while True:
for process in c.Win32_Process ():
id = process.ProcessID
for p in c.Win32_PerfRawData_PerfProc_Process (IDProcess=id):
n1, d1 = long (p.PercentProcessorTime), long
(p.Timestamp_Sys100NS)
n0, d0 = process_info.get (id, (0, 0))

try:
percent_processor_time = (float (n1 - n0) / float (d1 - d0)) *
100.0
except ZeroDivisionError:
percent_processor_time = 0.0
process_info[id] = (n1, d1)

if percent_processor_time > 0.01:
print "%20s - %2.3f" % (process.Caption, percent_processor_time)

print
time.sleep (5)

</code>

__________________________________________________ ______________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
__________________________________________________ ______________________
 
Reply With Quote
 
 
 
 
Gerrit Muller
Guest
Posts: n/a
 
      09-12-2006
[Tim Golden]
<nice answer snipped>

now I only have to find some time to play around...

thanks, Gerrit

--
Gaudi systems architecting:
<http://www.gaudisite.nl/>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
retrieving CPU Usage and Memory Usage information in JAVA hvt Java 0 03-13-2007 01:09 PM
retrieving CPU Usage and Memory Usage information in JAVA hvt Java 0 03-13-2007 01:07 PM
Java process profiling for CPU usage OtisUsenet Java 3 11-27-2006 01:59 AM
Get CPU usage of a single process in Windows Tor Erik Python 3 09-12-2006 09:52 AM
Cat 5500 Earl process is nearly 80% cpu usage phil41 Cisco 1 11-19-2005 04:14 PM



Advertisments