471,306 Members | 838 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,306 software developers and data experts.

Trying to find a simple way of getting a Process current CPU Usage

Hi all
I have been looking around the web
and can't seem to find a solution the solution that i have found and manipulated seems to bring back the whole list of currently running process's which hogs a lot of CPU time in its self..

basic idea of my program is i have multiple threads doing things and i have a thread that gathers data within this thread i can currently get all sorts of information using
Expand|Select|Wrap|Line Numbers
  1. b=str(gatherIt("firefox","Working Set"))
  2.  
  3. def gatherIt(whoIt,whatIt):
  4.     #this is the data gathering function thing
  5.     data = win32pdhutil.FindPerformanceAttributesByName(whoIt, counter=whatIt)
  6.     return data
  7.  
this then runs every 10 seconds and out puts the information along with a load of other data into a CSV file.
what i can't seem to do is get the cpu usage of a process.
the process i have found is this
Expand|Select|Wrap|Line Numbers
  1. import time
  2. import wmi,win32api,win32process
  3.  
  4.  
  5. c = wmi.WMI ()
  6. d=win32process.GetProcessTimes(1)
  7. print d
  8. process_info = {}
  9. while True:
  10.   for process in c.Win32_Process ():
  11.     if process.Caption == 'firefox.exe':
  12.       id = process.ProcessID
  13.       for p in c.Win32_PerfRawData_PerfProc_Process (IDProcess=id):
  14.         n1, d1 = long (p.PercentProcessorTime), long(p.Timestamp_Sys100NS)
  15.         n0, d0 = process_info.get (id, (0, 0))
  16.  
  17.       try:
  18.         percent_processor_time = (float (n1 - n0) / float (d1 - d0)) *100.0
  19.       except ZeroDivisionError:
  20.         percent_processor_time = 0.0
  21.       process_info[id] = (n1, d1)
  22.  
  23.       #if percent_processor_time > 0.01:
  24.       print process.Caption, percent_processor_time
  25.  
  26.     print
  27.   time.sleep (5)
  28.  
this seems to take ages to get a result and and if i limit it to that single process it prints out the information as if it was ignoring but still gathering the other process's information and it takes 50% CPU usage... not good
there must be a simpler way to get a single process's cpu usage
please help
Jan 9 '08 #1
3 5543
Hi all
i have managed to fix the 50% thing
bad coding had a while loop checking for the existence of something and no pause.. = loads of CPU resource.
i still need the %cpu-usage for a process any help would be great
Jan 10 '08 #2
Ok
I solved my own problem with a little help from SC (cheers Si)
worst thing is he doesn't even understand python... oh well here is the answer to get a process's CPU usage
Expand|Select|Wrap|Line Numbers
  1. import wmi,win32api,win32process
  2. c = wmi.WMI ()
  3. process_info = {}
  4.   for p in c.Win32_PerfRawData_PerfProc_Process (name='firefox'):
  5.     n1, d1 = long (p.PercentProcessorTime), long(p.Timestamp_Sys100NS)
  6.     n0, d0 = process_info.get (id, (0, 0))
  7.     try:
  8.       percent_processor_time = (float (n1 - n0) / float (d1 - d0)) *100.0
  9.     except ZeroDivisionError:
  10.       percent_processor_time = 0.0
  11.     process_info[id] = (n1, d1)
  12.     print p.Caption, percent_processor_time
  13.  
(NOTE: that you don't have to have the .exe at the end

simple really
I hope this helps someone else (this will be added to ASPN's python cook book.

cheers
D2
Jan 11 '08 #3
bvdet
2,851 Expert Mod 2GB
Ok
I solved my own problem with a little help from SC (cheers Si)
worst thing is he doesn't even understand python... oh well here is the answer to get a process's CPU usage
Expand|Select|Wrap|Line Numbers
  1. import wmi,win32api,win32process
  2. c = wmi.WMI ()
  3. process_info = {}
  4.   for p in c.Win32_PerfRawData_PerfProc_Process (name='firefox'):
  5.     n1, d1 = long (p.PercentProcessorTime), long(p.Timestamp_Sys100NS)
  6.     n0, d0 = process_info.get (id, (0, 0))
  7.     try:
  8.       percent_processor_time = (float (n1 - n0) / float (d1 - d0)) *100.0
  9.     except ZeroDivisionError:
  10.       percent_processor_time = 0.0
  11.     process_info[id] = (n1, d1)
  12.     print p.Caption, percent_processor_time
  13.  
(NOTE: that you don't have to have the .exe at the end

simple really
I hope this helps someone else (this will be added to ASPN's python cook book.

cheers
D2
Thanks for sharing this with the Forum. I wish I could have helped, but I know little about interfacing with the operating system.
Jan 11 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

7 posts views Thread by Joshua Beall | last post: by
108 posts views Thread by Bryan Olson | last post: by
4 posts views Thread by Greg B | last post: by
58 posts views Thread by sh.vipin | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.