Maybe I'm overlooking something, but I can't figure out how to get current_process.cpu_percent(interval=0.1)
for all processes at once without iterating over them. Currently iteration will take process_count * interval
seconds to finish. Is there a way around this?
So far I'm doing:
#!/usr/bin/env python2
import psutil
cpu_count = psutil.cpu_count()
processes_info = []
for proc in psutil.process_iter():
try:
pinfo = proc.as_dict(attrs=['pid', 'username', 'memory_info', 'cpu_percent', 'name'])
current_process = psutil.Process(pid=pinfo['pid'])
pinfo["cpu_info"] = current_process.cpu_percent(interval=0.1)
processes_info.append(pinfo)
except psutil.NoSuchProcess:
pass
print processes_info
As far as I understand it, I cannot include cpu-percent
into the attr
-list, since the help states
When interval is 0.0 or None compares system CPU times elapsed since last call or module import, returning immediately. That means the first time this is called it will return a meaningless 0.0 value which you are supposed to ignore.