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 OSOperating System module in Python provides an interface to interact with operating system commands.

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


# Signature
os.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: None is 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 module
import os
# Extract CPUs count
CPUs = os.cpu_count()
# print results
print("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 or None.
  • Line 7: We print the total count on the console.

Free Resources