Search code examples
pythontuplesextract

Extract certain value from scputimes tuple in python using psutil


I have been going around in circles for a few hours trying to work out how to extract data from a tuple. I found one way which worked for getting data from psutil.getloadavg but using that same thing does not work for psutil.cpu_times_percent.

So, lets say I run this command...

print(psutil.cpu_times_percent(interval=2))

I get the following returned...

scputimes(user=6.5, nice=0.0, system=4.5, idle=89.0, iowait=0.0, irq=0.0, softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0)

Now I want to extract just the idle number but how. I have tried the likes of but nothing has worked.

scpu = psutil.cpu_times_percent(interval=2)
for i, in scpu:
    print(i)

x=scpu[3]

Solution

  • Idle times spent in percentage is simply:

    psutil.cpu_times_percent(interval=2).idle