What is the psutil.cpu_stats() method in Python?
What is the psutil module?
The 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_stats method
The cpu_stats method is used to return the following CPU statistics attributes as a named tuple:
ctx_switches: This represents the number of context switches since the boot.interrupts: This represents the number of interrupts since boot.soft_interrupts: This represents the number of software interrupts since boot.syscalls: This represents the number of system calls since boot.
Syntax
psutil.cpu_stats()
Parameters
The method has no parameters.
Example
import psutilprint("The current stats of the CPU are as follows:")print(psutil.cpu_stats())
Explanation
- Line 1: We import
psutil. - Line 5: We print the output of the
cpu_stats()method.