Of course! In Python, you can use the psutil library to get the CPU's idle time. The idle time is the total amount of time the CPU has spent doing nothing (i.e., not executing any user or kernel code).

The key function for this is psutil.cpu_times().
Quick Answer
Here is the most direct way to get the CPU idle time in seconds:
import psutil
# Get detailed CPU times
cpu_times = psutil.cpu_times()
# The 'idle' attribute holds the idle time in seconds
idle_time = cpu_times.idle
print(f"CPU Idle Time: {idle_time} seconds")
Detailed Explanation
Understanding psutil.cpu_times()
The psutil.cpu_times() function returns a named tuple containing the amount of time the CPU has spent in each state. The available states can vary slightly depending on your operating system (Windows, macOS, Linux), but the most common ones are:
user: Time spent running user-space processes.system: Time spent running kernel-space processes.idle: Time spent idle. This is what you're looking for.nice: Time spent running user-space processes with low priority.iowait: Time spent waiting for I/O (like disk or network) to complete. (Common on Linux)irq/softirq: Time spent handling hardware/software interrupts. (Common on Linux)steal: Time spent in other operating systems in a virtualized environment. (Common on Linux)guest/guest_nice: Time spent running a virtual CPU for guest operating systems. (Common on Linux)
Example: Getting All CPU Times
It's often useful to see all the times to get a complete picture of CPU activity.

import psutil
# Get a snapshot of all CPU times
cpu_times = psutil.cpu_times()
print("--- CPU Times (in seconds) ---")
print(f"User: {cpu_times.user}")
print(f"System: {cpu_times.system}")
print(f"Idle: {cpu_times.idle}")
print(f"IOWait: {cpu_times.iowait}") # May be 0.0 on Windows
print(f"IRQ: {cpu_times.irq}") # May be 0.0 on Windows
print(f"Steal: {cpu_times.steal}") # May be 0.0 on non-virtualized systems
print("-" * 30)
# You can also access them by index, but named attributes are clearer
print(f"Idle time (by attribute): {cpu_times.idle}")
print(f"Idle time (by index): {cpu_times[2]}") # idle is usually at index 2
Getting Idle Time as a Percentage
Often, you're more interested in the percentage of time the CPU has been idle rather than the absolute seconds. You can calculate this yourself.
The formula is:
Percentage Idle = (Idle Time / Total Time) * 100
Where Total Time is the sum of all the CPU times.
import psutil
# Get CPU times
cpu_times = psutil.cpu_times()
# Calculate total time
total_time = sum(cpu_times)
# Calculate idle percentage
idle_percent = (cpu_times.idle / total_time) * 100
print(f"Total CPU time: {total_time} seconds")
print(f"CPU Idle Percentage: {idle_percent:.2f}%")
Monitoring Idle Time Over Time
To see how the idle time changes, you can take a measurement, wait a short period, take another measurement, and then calculate the difference.

import psutil
import time
def get_idle_diff(interval=1):
"""
Calculates the change in idle time over a given interval.
"""
# Get the first snapshot of idle time
start_idle = psutil.cpu_times().idle
# Wait for the specified interval
time.sleep(interval)
# Get the second snapshot of idle time
end_idle = psutil.cpu_times().idle
# The difference is how many seconds the CPU was idle during the interval
idle_diff = end_idle - start_idle
return idle_diff
# Example: Monitor for 5 seconds
print(f"Monitoring CPU idle time for 5 seconds in 1-second intervals...")
for i in range(5):
idle_seconds = get_idle_diff(interval=1)
idle_percent = (idle_seconds / 1) * 100 # Since interval is 1 second
print(f" Interval {i+1}: CPU was idle for {idle_seconds:.2f} seconds ({idle_percent:.2f}%)")
Important Consideration: cpu_percent()
It's worth noting that psutil provides a high-level function psutil.cpu_percent() which directly gives you the busy percentage of the CPU. You can easily derive the idle percentage from it.
import psutil
import time
# Wait a second to get a meaningful baseline
time.sleep(1)
# Get the percentage of CPU that was busy
busy_percent = psutil.cpu_percent(interval=1)
# Calculate idle percentage
idle_percent = 100 - busy_percent
print(f"CPU Busy: {busy_percent}%")
print(f"CPU Idle: {idle_percent}%")
This cpu_percent() method is often simpler and more direct for getting the percentage, but cpu_times().idle is the fundamental way to get the raw idle time in seconds.
