Write a python program to find the number of cpu count

Find number of CPUs using python :

Using python , we can easily check the number of CPUs available in a system. We will check two different methods to get this count :

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(). This is same as above i.e. both 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.

import multiprocessing
import os

print (multiprocessing.cpu_count())

print (os.cpu_count())
#print (len(os.sched_getaffinity(0)))

Leave a Reply