https://psutil.readthedocs.io/en/latest/index.html?highlight=oneshot#psutil.Process.oneshot
Utility context manager which considerably speeds up the retrieval of multiple process information at the same time. Internally different process info (e.g. name(), ppid(), uids(), create_time(), …) may be fetched by using the same routine, but only one value is returned and the others are discarded. When using this context manager the internal routine is executed once (in the example below on name()) the value of interest is returned and the others are cached. The subsequent calls sharing the same internal routine will return the cached value. The cache is cleared when exiting the context manager block. The advice is to use this every time you retrieve more than one information about the process. If you’re lucky, you’ll get a hell of a speedup.
I ran this:
import psutil
import time
p = psutil.Process(pid=10500)
with p.oneshot():
while p.status() == 'running':
print(p.name()) # execute internal routine once collecting multiple info
print(p.cpu_times()) # return cached value
print(p.memory_info().vms) # return cached value
print(p.cpu_percent()) # return cached value
print(p.create_time()) # return cached value
print(p.ppid()) # return cached value
print(p.status()) # return cached value
time.sleep(1)
On a multi-threaded python.exe (I'm on Windows). It returns information about the process and seems to be accurate when comparing with the Resource Monitor. What is it that you don't understand about your output?
Also, regarding cpu_times()
:
Return system CPU times as a named tuple. Every attribute represents
the seconds the CPU has spent in the given mode. The attributes
availability varies depending on the platform:
user: time spent by normal processes executing in user mode; on Linux
this also includes guest time
system: time spent by processes
executing in kernel mode
idle: time spent doing nothing
To find a process id (in this case we're looking for a python script) use:
{p.pid: p.info for p in psutil.process_iter() if p.info['name'] == 'python.exe'}
user: time spent in user mode.
system: time spent in kernel mode.
So be sure to use a pid, and you will be looking for the user
output, as you are running a user program.
This is my output for two iterations of monitoring a multi-threaded python.exe using the code from the first cell in this answer:
python.exe
pcputimes(user=8.546875, system=20.046875, children_user=0.0, children_system=0.0)
436965376
0.0
1642441782.2310572
15272
running
python.exe
pcputimes(user=8.546875, system=20.046875, children_user=0.0, children_system=0.0)
436965376
100.1
1642441782.2310572
15272
running
python.exe
pcputimes(user=8.546875, system=20.046875, children_user=0.0, children_system=0.0)
436965376
100.0
1642441782.2310572
15272
running
cpu_percent
is > 100 because I have multiple cores. One thing I should note, is that user
does not change in this loop. I'm running in a jupyter notebook cell and it only changes when I stop the cell and restart it. So it'll take some tinkering using the psutil lib.