Closed dorinclisu closed 10 months ago
This part:
for child in process.children(recursive=True):
cpu_usage += child.cpu_percent(interval=None)
...creates a set of brand new Process
instances, upon which you immediately call cpu_percent()
. They have no knowledge of what happened before, nor they have a big enough time window to calculate a meaningful percent value, hence they return 0.
From the doc:
the first time this cpu_percent() is called with interval = 0.0 or None it will return a meaningless 0.0 value which you are supposed to ignore
I was assuming there is a state stored outside the psutil.Process instance, since the pcputimes.user value is consistent with the actual child that exists before its psutil twin instantiation. And I could argue the documentation is confusing, because child.cpu_percent() is being called a first time (so as to ignore its meaningless 0.0 value), according to the PID, but not according to the psutil instance.
And I could argue the documentation is confusing, because child.cpu_percent() is being called a first time (so as to ignore its meaningless 0.0 value), according to the PID, but not according to the psutil instance.
I read it twice but I don't understand what you mean here. If you have suggestions on how to reword the doc please go for it.
Summary
Description
Replicable code:
Basically, cpu_usage works as expected for the main process, but not for the child spawned with subprocess.Popen, expecting ~100 but getting 0.0 instead.