I need to get CPU% for each process thread.
So, I am creating a simple script:
import psutil from psutil import Process p = psutil.Process(4499) treads_list = p.get_threads() for i in treads_list: o = i[0] th = psutil.Process(o) cpu_perc = th.get_cpu_percent(interval=1) print('PID %s use %% CPU = %s' % (o, cpu_perc))
Here's what TOP looks like for this process:
4942 teamcity 20 0 3288m 831m 3124 R 33.3 10.6 10303:37 java 32700 teamcity 20 0 3288m 831m 3124 S 5.9 10.6 18:49.99 java 5824 teamcity 20 0 3288m 831m 3124 S 5.9 10.6 1:57.90 java 4621 teamcity 20 0 3288m 831m 3124 S 3.0 10.6 1834:09 java 4622 teamcity 20 0 3288m 831m 3124 S 2.6 10.6 1844:15 java
The threads use 2.6-5.9% of the CPU, and the parent PID - use 33.3.
But - here is the result of the script:
It seems that each thread "feeds" 56-88% of the CPU ...
What am I missing here?
source share