What is the os.getloadavg() function in Python?

The os.getloadavg() function is used to extract the averages of the number of running processes over the last 1, 5, and 15 minutes, respectively. Load average is the mean value of processes executed over a period of time.

The Python OS module helps to interact with operating system command as an interface.

Syntax

# Signature
os.getloadavg()

Parameters

It does not take any argument value.

Return value

It returns a tuple containing three float values representing the load averages over the last 1, 5, and 15 minutes, respectively.

The getloadavg() function also throws an OSError exception when the load average is unobtainable, or the function fails to determine it.

Code example 1

# Program using getloadavg() function
# including os module
import os
# extracting load averages
values = os.getloadavg()
# print load averages of previous 1, 5 and 15 minutes
print("Load Averages:", values)

Explanation

In the above code snippet, we are invoking os.getloadavg() to get load averages over the last 1, 5, and 15 minutes.

  • Line 5: We invoke the getloadavg() function and store the returned tuple of averages in the values variable.
  • Line 7: We print the load average values on the console.

Code example 2

# Program using getloadavg() function
# including os module
import os
# extract load average values
Load1, Load5, Load15 = os.getloadavg()
# show the load averages
print("Load average over last 1 minute:", Load1)
print("Load average over last 5 minute:", Load5)
print("Load average over last 15 minute:", Load15)

Explanation

  • Line 5: We invoke the os.getloadavg() function and store the load averages in Load1 (load average of the last 1 minute), Load5 (load average of the last 5 minutes) and Load15 (load average of the last 15 minutes) separately.
  • Lines 7–9: We print the load average values with relevant messages.

Free Resources