devenh
Posts: 32
Joined: Fri Jun 05, 2015 9:11 pm

Determining Python CPU usage

Tue Oct 25, 2016 7:03 pm

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?

DirkS
Posts: 9972
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Determining Python CPU usage

Tue Oct 25, 2016 7:15 pm

Maybe you can use psutil's process.cpu_percent?
https://pythonhosted.org/psutil/#psutil ... pu_percent

User avatar
rpdom
Posts: 15430
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Determining Python CPU usage

Tue Oct 25, 2016 7:21 pm

Does the "top" command help?

devenh
Posts: 32
Joined: Fri Jun 05, 2015 9:11 pm

Re: Determining Python CPU usage

Tue Oct 25, 2016 8:42 pm

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.

Return to “Python”