How to find out the number of CPUs using python

0 votes
asked Jun 17, 2009 by phihag

I want to know the number of CPUs on the local machine using Python. The result should be user/real as output by time(1) when called with an optimally scaling userspace-only program.

8 Answers

0 votes
answered Jun 17, 2009 by nadia-alramli

If you have python with a version >= 2.6 you can simply use

import multiprocessing


0 votes
answered Jun 2, 2010 by ben-scherrey

Can't figure out how to add to the code or reply to the message but here's support for jython that you can tack in before you give up:

# jython
    from java.lang import Runtime
    runtime = Runtime.getRuntime()
    res = runtime.availableProcessors()
    if res > 0:
        return res
except ImportError:
0 votes
answered Jun 12, 2013 by bakuriu

Another option is to use the psutil library, which always turn out useful in these situations:

>>> import psutil
>>> psutil.cpu_count()

This should work on any platform supported by psutil(Unix and Windows).

Note that in some occasions multiprocessing.cpu_count may raise a NotImplementedError while psutil will be able to obtain the number of CPUs. This is simply because psutil first tries to use the same techniques used by multiprocessing and, if those fail, it also uses other techniques.

0 votes
answered Jun 3, 2014 by jfs

In Python 3.4+: os.cpu_count().

multiprocessing.cpu_count() is implemented in terms of this function but raises NotImplementedError if os.cpu_count() returns None ("can't determine number of CPUs").

0 votes
answered Jun 17, 2014 by douglas-b-staple

multiprocessing.cpu_count() will return the number of logical CPUs, so if you have a quad-core CPU with hyperthreading, it will return 8. If you want the number of physical CPUs, use the python bindings to hwloc:

#!/usr/bin/env python
import hwloc
topology = hwloc.Topology()
print topology.get_nbobjs_by_type(hwloc.OBJ_CORE)

hwloc is designed to be portable across OSes and architectures.

0 votes
answered Jun 29, 2014 by alkero

Another option if you don't have Python 2.6:

import commands
n = commands.getoutput("grep -c processor /proc/cpuinfo")
0 votes
answered Jun 21, 2015 by amit12690

You can also use "joblib" for this purpose.

import joblib
print joblib.cpu_count()

This method will give you the number of cpus in the system. joblib needs to be installed though. More information on joblib can be found here

Alternatively you can use numexpr package of python. It has lot of simple functions helpful for getting information about the system cpu.

import numexpr as ne
print ne.detect_number_of_cores()
0 votes
answered Jun 11, 2016 by davoud-taghawi-nejad

platform independent:


Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter