How to capture the CPU core usage from system monitor program?
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 m开发者_C百科odule or tool which allows me to capture the individual core usage from the system monitor program when I run my job?
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]
import psutil
values = psutil.cpu_percent(percpu=True)
print values
[0.0, 0.0, 0.0]
values = psutil.cpu_percent()
print values
0.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.
精彩评论