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

Nicholas Bastin nick.bastin at gmail.com
Sun Oct 7 20:26:18 EDT 2007


On 10/6/07, Lawrence Oluyede <raims at dot.com> wrote:
> John <weekender_ny at yahoo.com> wrote:
> > Is there a way to find the number of processors on a machine (on linux/
> > windows/macos/cygwin) using python code (using the same code/cross
> > platform code)?
>
> >From processing <http://cheeseshop.python.org/pypi/processing/0.34> :

A few reiterated notes inline, from my previous message.

> def cpuCount():
>     '''
>     Returns the number of CPUs in the system
>     '''
>     if sys.platform == 'win32':
>         try:
>             num = int(os.environ['NUMBER_OF_PROCESSORS'])

The user can do bad things to this environment variable, but it's
probably ok most of the time. (Hey, they change it, they pay the
consequences).

>     else:
>         try:
>             num = os.sysconf('SC_NPROCESSORS_ONLN')

This is really bad on linux.  You really want to parse /proc/cpuinfo
because HT 'cpus' are almost useless, and can be bad in situations
where you try to treat them as general purpose cpus.

--
Nick



More information about the Python-list mailing list