Page 1 of 1

Determining Python CPU usage

Posted: Tue Oct 25, 2016 7:03 pm
by devenh
I'd like to monitor the CPU usage for my Python app. I can get total CPU usage and CPU usage for each of the four CPUs. I'm not interested in total usage. I'm interested in CPU usage by core, but I don't know which of the four CPUs Python is running on. How can I determine this?

Re: Determining Python CPU usage

Posted: Tue Oct 25, 2016 7:15 pm
by DirkS
Maybe you can use psutil's process.cpu_percent?
https://pythonhosted.org/psutil/#psutil ... pu_percent

Re: Determining Python CPU usage

Posted: Tue Oct 25, 2016 7:21 pm
by rpdom
Does the "top" command help?

Re: Determining Python CPU usage

Posted: Tue Oct 25, 2016 8:42 pm
by devenh
process.cpu_percent is what I am looking for ... thanks.

With psutil.Process() you can get the PID. I would think the process is run on a single core, but I don't see a way in psutil to identify the core.

I wrote some code to query /proc/stat that shows utilization by core. Interestingly, this shows total utilization and utilization by core to be approximately 40% each when my app is running. However, process.cpu_percent shows 80% which is obviously about twice that of any given core.

My understanding is that process.cpu_percent can be greater than 100% for multi threaded apps. If my app was multi threaded, then I could understand the 80% figure, but unless some of the libraries I'm using are multi threaded, I'm a bit puzzled it is so much higher than the core numbers.

I did compare the 80% utilization to the Task Manager and the Task Manager figure was always 1/4th what process.cpu_percent reported. Looks like TM is showing total system utilization (with four cores) as opposed to core utilization.