Cross platform way of finding number of processors on a machine?
nick at craig-wood.com
Sat Oct 6 20:30:13 CEST 2007
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> :
> def cpuCount():
> Returns the number of CPUs in the system
> if sys.platform == 'win32':
> num = int(os.environ['NUMBER_OF_PROCESSORS'])
> except (ValueError, KeyError):
> elif sys.platform == 'darwin':
> num = int(os.popen('sysctl -n hw.ncpu').read())
> except ValueError:
> num = os.sysconf('SC_NPROCESSORS_ONLN')
> except (ValueError, OSError, AttributeError):
> if num >= 1:
> return num
> raise NotImplementedError
There is a bug in that code...
NotImplementedError will never be raised because num won't have been
set. It will raise "UnboundLocalError: local variable 'num'
referenced before assignment" instead
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick
More information about the Python-list