The easiest way to get the number of cores in Python is to use the cpu_count() function from the Python os module.

import os

print(os.cpu_count())

#Output:
4

The Python os module gives us many useful functions for interacting with a computer’s operating system to gather information and make changes.

One piece of information which can be very useful is the number of processing cores or CPUs that a computer has.

We can easily get the number of cores using the os module cpu_count() function.

You can print the number of cores in your system using the following Python code.

import os

print(os.cpu_count())

#Output:
4

If Python cannot determine how many cores are in your system, then cpu_count() will return None.

Hopefully this article has been useful for you to learn how you can get the number of processing cores your computer and system has using Python.

Categorized in:

Python,

Last Update: February 26, 2024