Last Updated on June 27, 2022
You can determine the number of CPUs in your system using the multiprocessing.cpu_count() function or the os.cpu_count() function.
In this tutorial you will discover how to count the number of CPUs in Python.
Let’s get started.
Table of Contents
Need to Know The Number of CPUs
In concurrent programming, we often need to know the number of CPUs available to the program.
There are many reasons for this, such as:
- Configuring a thread pool or a process pool.
- Configuring the number of concurrent tasks.
- Estimating the time a program will take to run.
How can we determine the number of CPU cores from within the program?
What Do We Mean By CPU
Before we look at how to determine the number of CPU cores in a program, let’s get clear on what we mean by a CPU and a core and the difference between physical cores and logical cores.
CPU vs Core
A central processing unit or simply “processor” or “CPU” is a chip in the computer that executes instructions.
Traditionally, we had one CPU in the computer, perhaps with a math coprocessor.
- CPU: Central processing unit, a chip within the computer for executing instructions.
A core is another name for a physical CPU for executing instructions.
A computer with multiple CPUs is referred to as having multiple cores.
Similarly, a computer chip that has multiple CPUs within it, is referred to as a multi-core processor.
- Multi-core processor: A physical chip with multiple CPUs or cores.
As such, as developers, the terms “CPUs” and “cores” are used interchangeably. We might even refer to them as “CPU cores”.
Almost all modern computers have multiple cores.
Physical vs Logical CPUs
There is one further wrinkle.
Modern CPUs typically make use of a technology called hyperthreading.
Hyperthreading does not refer to a program using threads. Instead, it refers to a technology within the CPU cores themselves that allows each physical core or CPU to act as if it were two logical cores or two CPUs.
- Physical Cores: The number of CPU cores provided in the hardware, e.g. the chips.
- Logical Cores: The number of CPU cores after hyperthreading is taken into account.
It provides automatic in-core parallelism that can offer up to a 30% speed-up over CPU cores that do not offer the technology.
As such, when we count CPU cores in a system, we typically count the number of logical CPU cores, not the number of physical CPU cores.
If you know your system uses hyperthreading (it probably does), then you can get the number of physical CPUs in your system by dividing the number of logical CPUs by two.
- Count Physical Cores = Count Logical Cores / 2
Next, let’s look at how we can get the number of CPUs in Python.
How to Get The Number of CPUs in Python
There are a number of ways to get the number of CPUs in Python.
The two pure Python approaches provided by the standard library are:
- multiprocessing.cpu_count() function
- os.cpu_count() function.
Let’s take a closer look at each in turn.
How to Use multiprocessing.cpu_count
We can get the count of the number of logical CPU cores in the current system using the multiprocessing.cpu_count() function.
It returns a positive integer.
For example:
1 2 3 |
... # get the number of logical cpu cores n_cores = multiprocessing.cpu_count() |
How to Use os.cpu_count
We can also use the os.cpu_count() function to return the count of the number of logical CPU cores in the current system.
For example:
1 2 3 |
... # get the number of logical cpu cores n_cores = os.cpu_count() |
There are many more platform specific techniques we could use, such as reading specific files or checking specific environment variables.
Nevertheless, in this tutorial we are focused on pure Python platform agnostic methods we can use to determine the number of CPUs in the system.
Now that we know how to get the number of CPUs in Python, let’s look at some worked examples.
CPU Count with multiprocessing.cpu_count()
We can get the count of the number of CPUs in your system using the multiprocessing.cpu_count() function.
This function will return the number of logical CPUs in your system as an integer. If the number of CPUs cannot be determined, then the function will return the value None.
The example below gets and then reports the number of logical CPUs in the system.
1 2 3 4 5 6 7 |
# SuperFastPython.com # example getting the number of cpu cores from multiprocessing import cpu_count # get the number of logical cpu cores n_cores = cpu_count() # report the number of logical cpu cores print(f'Number of Logical CPU cores: {n_cores}') |
Running the example reports the total number of logical CPU cores in the system.
In this case, there are 8 logical CPU cores. My system has hyperthreading, which means there are 4 physical CPU cores.
Note, your specific results may differ based on the number of CPU cores you have in your system.
1 |
Number of Logical CPU cores: 8 |
How many CPU cores do you have? Let me know in the comments below.
Next, let’s consider an alternate way to get the number of CPU cores.
CPU Count with os.cpu_count()
We can get the count of the number of CPUs in your system using the os.cpu_count() function.
This function will return the number of logical CPUs in your system as an integer. If the number of CPUs cannot be determined, then the function will return the value None.
In fact, the multiprocessing.cpu_count() simply calls the os.cpu_count() function.
The example below gets and then reports the number of logical CPUs in the system.
1 2 3 4 5 6 7 |
# SuperFastPython.com # example getting the number of cpu cores from os import cpu_count # get the number of logical cpu cores n_cores = cpu_count() # report the number of logical cpu cores print(f'Number of Logical CPU cores: {n_cores}') |
Running the example reports the total number of logical CPU cores in the system.
In this case, there are 8 logical CPU cores. My system has hyperthreading, which means there are 4 physical CPU cores.
Note, your specific results may differ based on the number of CPU cores you have in your system.
1 |
Number of Logical CPU cores: 8 |
How many CPU cores do you have? Let me know in the comments below.
Next, let’s consider the number of CPU cores available to your process.
Number of Available CPU Cores
We can also get the total number of CPUs that can be used by the current Python process.
The os.sched_getaffinity() function returns the details of the number of CPU cores available to the current process.
Note, this function is only available on some Unix platforms, e.g. not Windows or macOS.
We pass the PID of zero, to report the details of all available CPUs for the current process. The result is a list of the details for the available CPU cores, of which we can then report the length to get the number of available cores.
This is typically all of the CPU cores of the system, although may have been limited by the operating system, other software, or by the Python program itself via a library call.
For example:
1 2 3 |
... # get the number of available cpu cores n_available_cores = len(os.sched_getaffinity(0)) |
The example below reports the number of available CPU cores.
1 2 3 4 5 6 7 |
# SuperFastPython.com # example getting the number of available cpu cores from os import sched_getaffinity # get the number of available logical cpu cores n_available_cores = len(sched_getaffinity(0)) # report the number of logical cpu cores print(f'Number of Available CPU cores: {n_available_cores}') |
Running the example may report the number of CPU cores available to your process.
This is probably the total number of logical CPU cores in the system.
The os.sched_getaffinity() function is not available on all platforms, e.g. not macOS and not windows, in which case running the above example will result in an error as follows:
1 |
ImportError: cannot import name 'sched_getaffinity' from 'os' |
You can learn more about multiprocessing in the tutorial:
Takeaways
You now know how to get the number of CPUs in Python.
Do you have any questions?
Ask your questions in the comments below and I will do my best to answer.
Photo by Nick Fewings on Unsplash
Do you have any questions?