Cross platform way of finding number of processors on a machine?

Paul Boddie paul at boddie.org.uk
Sat Oct 6 09:26:06 EDT 2007


On 6 Okt, 12:45, ra... at dot.com (Lawrence Oluyede) wrote:
>
> From processing <http://cheeseshop.python.org/pypi/processing/0.34> :

[...]

>             num = os.sysconf('SC_NPROCESSORS_ONLN')

It's interesting what new (or obscure) standard library functions (and
system functions) can be discovered through these kinds of
discussions. However, this one seems to report the number of "virtual
CPUs" in situations like mine where the CPU supports hyperthreading,
as Nicholas Bastin says. One way to get the real number of CPUs (on
Linux-based systems) is to parse /proc/cpuinfo, and to count the
number of distinct "physical id" values.

>From the experiments I've done with pprocess [1], pretending that a
hyperthreading "virtual CPU" is as good as a real CPU (or CPU core)
tends to be counter-productive: the performance benefits in moving
from the utilisation of one to two "virtual CPUs" are virtually non-
existent.

Paul

[1] http://www.python.org/pypi/pprocess




More information about the Python-list mailing list