473,326 Members | 2,732 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 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 5869
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

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

Similar topics

7
by: Joshua Beall | last post by:
Hi All, I am doing some work where I want to do locking, and prevent scripts from running in parallel. I see that I could use the semaphore mechanism, but I'd like for my code to be portable,...
108
by: Bryan Olson | last post by:
The Python slice type has one method 'indices', and reportedly: This method takes a single integer argument /length/ and computes information about the extended slice that the slice object would...
4
by: David Helgason | last post by:
What: I'm having trouble finding out how to find the current PGconn connection inside a C function. Looking through the documentation didn't give this up. Could anyone suggest where to look? I...
4
by: Greg B | last post by:
Well since getopt() doesn't seem to be compatible with Windows, and the free implementation of it for Windows that I found still had some annoying restrictions, I thought I'd whip up a simple...
5
by: tshad | last post by:
I am trying to access my log files and am running into a permissions problem. I am doing the following: ****************************************************************** private void...
35
by: Alex Martelli | last post by:
Having fixed a memory leak (not the leak of a Python reference, some other stuff I wasn't properly freeing in certain cases) in a C-coded extension I maintain, I need a way to test that the leak is...
5
by: snicks | last post by:
I'm trying to exec a program external to my ASP.NET app using the following code. The external app is a VB.NET application. Dim sPPTOut As String sPPTOut = MDEPDirStr + sID + ".ppt" Dim p As...
4
by: NancyJ | last post by:
Currently we have a database with a main table containing 3 million records - we want to increase that to 10 million but thats not a possibility at the moment. Nearly all 3 million records are...
58
by: sh.vipin | last post by:
is there any way to find out number of bytes freed on a particular free() call in C
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.