What is the psutil.cpu_freq() method?

What is the psutil module?

In Python, psutil (Python system and process utilities) is a Python package that retrieves information on ongoing processes and system usage (CPU, memory, storage, network, and sensors). It is mostly used for system monitoring, profiling, restricting process resources, and process management.

The module can be installed via pip, as follows:

pip install psutil

The cpu_freq method

The cpu_freq method returns the CPU frequency as a named tuple with the following attributes:

  • current: This represents the current CPU frequency.
  • min: This represents the minimum CPU frequency.
  • max: This represents the maximum CPU frequency.

All attributes above are reported in Mhz. If min and max values cannot be determined, they are set to zero.

Syntax

psutil.cpu_freq(percpu=False)
  • percpu: We use this if the system supports the per-CPU frequency. Setting percpu to True makes the method return the CPU frequency of every CPU.

The method has no parameters.

Code

import psutil
print("The CPU frequency of the current CPU:")
print(psutil.cpu_freq())
print("The CPU frequency of all the CPUs:")
print(psutil.cpu_freq(True))

Explanation

  • Line 1: We import the psutil module.
  • Line 4: We retrieve the CPU frequency of the current CPU.
  • Line 8: We retrieve the CPU frequency of all the CPUs.

Free Resources