Write a python program to find the number of CPU count
Find the number of CPUs using python :
Using python, we can easily check the number of CPUs available in a system. In this example program, we will learn two different methods to get this count.
What is CPU numbers :
CPU or central processing unit is the main processing unit in a computer that handles all computational works. A CPU can contain only one single core or multiple cores. Single core CPUs are rare these days and they are slower than multi-core units. A single core CPU can work on only one process but a multi-core CPU can work on multiple tasks simultaneously. In simple words, multi-core CPUs can provide a better result on multitasking.
For example, a dual-core CPU appears to the operating system as two separate CPUs. Actually, the computer has only one CPU, but it appears like two. Similarly, a quad-core CPU has four central processing units and an octa-core CPU has eight processing units.
You can do multitasking on a single core processor but the speed will be significantly low than any other multi-core processor. It will give you an illusion that more than one process is running at the same time, but actually, the CPU will work one process at a time. It will keep switching between processes to create the illusion.
Multiple core CPUs can work on multiple processes at the same time. For example, a dual-core process can work on two processes parallel at the same time. It will make the system faster than a single core CPU.
So, systems have actually one physical CPU unit with multiple cores in it. Previously multiple physical CPUs were used before multiple cores were introduced, but that required multiple ports for each CPU on the motherboard, space for each CPU and a lot of other issues. Using multiple cores helps us to keep the CPU unit small and it requires less space and fewer ports.
In this tutorial, we are going to learn how to get the total CPU count in the system using python. Both are one-liner programs as we are only printing out the count of total CPUs. Note the output of the program will be different on different systems. This program will come in handy if you are writing code that behaves differently with platforms of different CPU count.
Python program to find the CPU number :
We will show you two different methods to find out the CPU count. Both methods will print the same output in the same machine.
Method 1 :
Using os.cpu_count() . It returns the number of CPUs in the system and ‘None’ if undetermined.
Method 2 :
Using multiprocessing.cpu_count(). The output is equal to the previous example i.e. both methods will return the same value.
Both of these numbers are not equivalent to the number of CPUs the current process can use. To get the number of usable CPUs, we can use len(os.sched_getaffinity(0)). But this value is available on some Unix platforms.
Python program :
import multiprocessing import os print (multiprocessing.cpu_count()) print (os.cpu_count()) #print (len(os.sched_getaffinity(0)))
You can also download this program from here.
Sample Output :
As you can see that the values are the same for all these three methods.