Last Updated on September 12, 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.
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?
Run loops using all CPUs, download your FREE book to learn how.
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.
Free Python Multiprocessing Course
Download your FREE multiprocessing PDF cheat sheet and get BONUS access to my free 7-day crash course on the multiprocessing API.
Discover how to use the Python multiprocessing module including how to create and start child processes and how to use a mutex locks and semaphores.
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.
Overwhelmed by the python concurrency APIs?
Find relief, download my FREE Python Concurrency Mind Maps
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:
Further Reading
This section provides additional resources that you may find helpful.
Python Multiprocessing Books
- Python Multiprocessing Jump-Start, Jason Brownlee (my book!)
- Multiprocessing API Interview Questions
- Multiprocessing API Cheat Sheet
I would also recommend specific chapters in the books:
- Effective Python, Brett Slatkin, 2019.
- See: Chapter 7: Concurrency and Parallelism
- High Performance Python, Ian Ozsvald and Micha Gorelick, 2020.
- See: Chapter 9: The multiprocessing Module
- Python in a Nutshell, Alex Martelli, et al., 2017.
- See: Chapter: 14: Threads and Processes
Guides
- Python Multiprocessing: The Complete Guide
- Python Multiprocessing Pool: The Complete Guide
- Python ProcessPoolExecutor: The Complete Guide
APIs
References
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
Narayan says
Hi Jason,
The above methods may not hold true with modern hybrid CPUs (mainly from Intel who invented the idea), where the P cores support HT whereas the E cores don’t.
So, I’ve switched to using psutil.cpu_count(logical=False) to get the actual number of cores available in the CPU and it works well across the board, with Intel & AMD CPUs.
Narayan
Jason Brownlee says
Thank you for sharing.
JackTatt says
My Dell XPS has 12 Intel cores, which is 16 logical processors. Does that mean not all cores have hyprethreading? Which number should I use for the max workers allowed using concurrency.futures.ProcessPoolExecutor for my CPU-bound code.
Jason Brownlee says
Fascinating! Yes, I guess that means not all cores have hyperthreading.
Great question. Start with 12 and benchmark. Then try more, such as 15 or 16, benchmark again and compare performance.
Let me know how you go.