Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

os module
python programming
communitycreator

What is os.cpu_count() method in Python?

Fouzia Bashir

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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)
Calculating the CPU count

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.

RELATED TAGS

os module
python programming
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring