6

I have a python program running using a parallel python. This module scales my job submitted to individual cores of my computer , I don't know how does it do it but when i check my system monitor it clearly shows 100% usage in both the cores (I am running some really heavy jobs).

Is there any python module or tool which allows me to capture the individual core usage from the system monitor program when I run my job?

3 Answers 3

13

psutil:

>>> for x in range(3):
...     psutil.cpu_percent(interval=1, percpu=True)
... 
[4.0, 6.9]
[7.0, 8.5]
[1.2, 9.0]
Sign up to request clarification or add additional context in comments.

Comments

2
import psutil 

values = psutil.cpu_percent(percpu=True) 
print values 

[0.0, 0.0, 0.0] 

values = psutil.cpu_percent() 
print values 

0.0

1 Comment

Welcome to Stack Overflow. Please review How to Answer. Code only answers aren't as helpful as ones which provide an explanation of what they are doing.
0

So if you have a way to call the system monitor to write the core usage to stdout, you can always do something like this:

import subprocess
command = 'top -b -n 1'  # or whatever you use
output = subprocess.Popen(command, stdout=subprocess.PIPE).communicate[0]

# parse output here and extract cpu usage. this is super-dependent
# on the layout of your system monitor output
cpuline = output.split('\n')[2]

You can read up on how subprocess works here.

3 Comments

i want to output the content from the system monitor , i don`t have a way to call the system monitor.
How do you check your system monitor then? Somewhere down the line you must call the system monitor program to start it, in which case you should be able to call that. Or is it already running in the background? Then there may exist some way to query the running process for its current state.
try giving a clean answer by using libraries please, piping is hacky imo.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.