What is os.cpu_count() method in Python?
Overview
os.cpu_count() is used to get the number of CPUs in a system. It does not take any argument value and returns the total CPU count as an integer. Otherwise, it returns none.
The
Note: It does not return the count of CPUs that can be used by the current process. Instead, it returns the count of the total CPUs in a system.
Syntax
# Signatureos.cpu_count()
Return values
It returns the following values.
int: This refers to the number of CPUs in a system as an integer value.None:Noneis returned if the number of CPUs is undetermined.
Example code
Let's take a look at a code example.
# Program for os.cpu_count()# import os moduleimport os# Extract CPUs countCPUs = os.cpu_count()# print resultsprint("Total CPUs count:", CPUs)
Explanation
In the code snippet above, we invoke the cpu_count() from the OS module to extract the count of CPUs.
- Line 5: We call the
cpu_count()to get either the CPUs in the system as an integer value orNone. - Line 7: We print the total count on the console.