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

Lawrence Oluyede raims at dot.com
Sat Oct 6 06:45:16 EDT 2007


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':
        try:
            num = int(os.environ['NUMBER_OF_PROCESSORS'])
        except (ValueError, KeyError):
            pass
    elif sys.platform == 'darwin':
        try:
            num = int(os.popen('sysctl -n hw.ncpu').read())
        except ValueError:
            pass
    else:
        try:
            num = os.sysconf('SC_NPROCESSORS_ONLN')
        except (ValueError, OSError, AttributeError):
            pass
        
    if num >= 1:
        return num
    else:
        raise NotImplementedError

-- 
Lawrence, oluyede.org - neropercaso.it
"It is difficult to get a man to understand 
something when his salary depends on not
understanding it" - Upton Sinclair



More information about the Python-list mailing list