Friday, May 30, 2008

Detecting cpus/cores in Python

I found this interesting function in a post on Twister and distributed programming by Bruce Eckel. It uses the Python os package to detect the number of CPUs/cores on a machine. Archiving it here for future reference

def detectCPUs():
"""
Detects the number of CPUs on a system. Cribbed from pp.
"""
# Linux, Unix and MacOS:
if hasattr(os, "sysconf"):
if os.sysconf_names.has_key("SC_NPROCESSORS_ONLN"):
# Linux & Unix:
ncpus = os.sysconf("SC_NPROCESSORS_ONLN")
if isinstance(ncpus, int) and ncpus > 0:
return ncpus
else: # OSX:
return int(os.popen2("sysctl -n hw.ncpu")[1].read())
# Windows:
if os.environ.has_key("NUMBER_OF_PROCESSORS"):
ncpus = int(os.environ["NUMBER_OF_PROCESSORS"]);
if ncpus > 0:
return ncpus
return 1 # Default

6 comments:

Anonymous said...

If you are using the multiprocessing module, do this instead:
multiprocessing.cpu_count()

Raghavan said...

Thanks Felix. I happened to be using python 2.5 when posting this. multiprocessing seems to have been introduced from 2.6 onwards.

Anonymous said...

Multiprocessing has been backported to 2.4/2.5, see

http://pypi.python.org/pypi/multiprocessing/

Ben FrantzDale said...

The processing module also seems to provide this:

import processing
processing.cpuCount()

Raghavan said...

@anonymous : thanks !

@ben : Is processing part of the standard library ? unless i am mistaken this needs to be downloaded from pypi ?

Unknown said...

For python 2.6 it is

import multiprocessing
multiprocessing.cpu_count()