How to Find Out the Number of CPUs in Python
Created
Modified
Using multiprocessing Module
The multiprocessing.cpu_count()
method returns the number of CPUs in the system. For example,
#!/usr/bin/python3
# Import module
import multiprocessing
cpus = multiprocessing.cpu_count()
print(cpus)
4
When the number of CPUs cannot be determined a NotImplementedError is raised.
Using os Module
The os.cpu_count()
method returns the number of CPUs in the system. Returns None if undetermined.
The following example should cover whatever you are trying to do:
#!/usr/bin/python3
# Import module
import os
cpus = os.cpu_count()
print(cpus)
4