How to get the hardware's fan speed in Python
We can retrieve our hardware’s fan speed using the psutil module in Python.
What is the psutil module?
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 sensors_fans method
The sensors_fans method returns the available fans and their respective speed in revolutions per minute (RPM).
Syntax
psutil.sensors_fans()
This method has no parameters.
Code
import psutilprint("The hardware fan speed is:")print(psutil.sensors_fans())# If there are no fan sensors in the machine,# then the output would be an empty dictionary.# If there are fan sensors,# then the fan speed would be displayed.
Code explanation
- Line 1: We import the
psutilmodule. - Line 2: We display a “The hardware fan speed is:” message.
- Line 5: We retrieve information about the fans and their speeds using the
sensors_fans()method.