如何使用python查找cpu的数量

时间:2021-05-17 07:14:46

I want to know the number of CPUs on the local machine using Python. The result should be user/real as output by time(1) when called with an optimally scaling userspace-only program.

我想知道使用Python的本地机器上cpu的数量。当使用最优的扩展用户空间的程序调用时,结果应该是user/real作为时间(1)的输出。

10 个解决方案

#1


595  

If you have python with a version >= 2.6 you can simply use

如果您有python版本的>= 2.6,您可以简单地使用。

import multiprocessing

multiprocessing.cpu_count()

http://docs.python.org/library/multiprocessing.html#multiprocessing.cpu_count

http://docs.python.org/library/multiprocessing.html multiprocessing.cpu_count

#2


139  

If you're interested into the number of processors available to your current process, you have to check cpuset first. Otherwise (or if cpuset is not in use), multiprocessing.cpu_count() is the way to go in Python 2.6 and newer. The following method falls back to a couple of alternative methods in older versions of Python:

如果您对当前进程可用的处理器数量感兴趣,您必须首先检查cpuset。否则(或者如果没有使用cpuset),那么multiprocess .cpu_count()是Python 2.6中更新的方法。下面的方法可以追溯到Python的旧版本中的几个替代方法:

import os
import re
import subprocess


def available_cpu_count():
    """ Number of available virtual or physical CPUs on this system, i.e.
    user/real as output by time(1) when called with an optimally scaling
    userspace-only program"""

    # cpuset
    # cpuset may restrict the number of *available* processors
    try:
        m = re.search(r'(?m)^Cpus_allowed:\s*(.*)$',
                      open('/proc/self/status').read())
        if m:
            res = bin(int(m.group(1).replace(',', ''), 16)).count('1')
            if res > 0:
                return res
    except IOError:
        pass

    # Python 2.6+
    try:
        import multiprocessing
        return multiprocessing.cpu_count()
    except (ImportError, NotImplementedError):
        pass

    # https://github.com/giampaolo/psutil
    try:
        import psutil
        return psutil.cpu_count()   # psutil.NUM_CPUS on old versions
    except (ImportError, AttributeError):
        pass

    # POSIX
    try:
        res = int(os.sysconf('SC_NPROCESSORS_ONLN'))

        if res > 0:
            return res
    except (AttributeError, ValueError):
        pass

    # Windows
    try:
        res = int(os.environ['NUMBER_OF_PROCESSORS'])

        if res > 0:
            return res
    except (KeyError, ValueError):
        pass

    # jython
    try:
        from java.lang import Runtime
        runtime = Runtime.getRuntime()
        res = runtime.availableProcessors()
        if res > 0:
            return res
    except ImportError:
        pass

    # BSD
    try:
        sysctl = subprocess.Popen(['sysctl', '-n', 'hw.ncpu'],
                                  stdout=subprocess.PIPE)
        scStdout = sysctl.communicate()[0]
        res = int(scStdout)

        if res > 0:
            return res
    except (OSError, ValueError):
        pass

    # Linux
    try:
        res = open('/proc/cpuinfo').read().count('processor\t:')

        if res > 0:
            return res
    except IOError:
        pass

    # Solaris
    try:
        pseudoDevices = os.listdir('/devices/pseudo/')
        res = 0
        for pd in pseudoDevices:
            if re.match(r'^cpuid@[0-9]+$', pd):
                res += 1

        if res > 0:
            return res
    except OSError:
        pass

    # Other UNIXes (heuristic)
    try:
        try:
            dmesg = open('/var/run/dmesg.boot').read()
        except IOError:
            dmesgProcess = subprocess.Popen(['dmesg'], stdout=subprocess.PIPE)
            dmesg = dmesgProcess.communicate()[0]

        res = 0
        while '\ncpu' + str(res) + ':' in dmesg:
            res += 1

        if res > 0:
            return res
    except OSError:
        pass

    raise Exception('Can not determine number of CPUs on this system')

#3


60  

Another option is to use the psutil library, which always turn out useful in these situations:

另一个选择是使用psutil库,它在这些情况下总是有用的:

>>> import psutil
>>> psutil.cpu_count()
2

This should work on any platform supported by psutil(Unix and Windows).

这应该适用于psutil(Unix和Windows)支持的任何平台。

Note that in some occasions multiprocessing.cpu_count may raise a NotImplementedError while psutil will be able to obtain the number of CPUs. This is simply because psutil first tries to use the same techniques used by multiprocessing and, if those fail, it also uses other techniques.

注意,在某些情况下,多处理。cpu_count可以增加一个NotImplementedError,而psutil可以获得cpu的数量。这仅仅是因为psutil首先尝试使用多处理所使用的相同技术,如果这些技术失败,它还会使用其他技术。

#4


21  

In Python 3.4+: os.cpu_count().

在Python 3.4 +:os.cpu_count()。

multiprocessing.cpu_count() is implemented in terms of this function but raises NotImplementedError if os.cpu_count() returns None ("can't determine number of CPUs").

cpu_count()是根据这个函数实现的,但如果os.cpu_count()返回None(“无法确定cpu的数量”),则不会引发NotImplementedError。

#5


18  

platform independent:

平台无关的:

psutil.cpu_count(logical=False)

psutil.cpu_count(逻辑= False)

https://github.com/giampaolo/psutil/blob/master/INSTALL.rst

https://github.com/giampaolo/psutil/blob/master/INSTALL.rst

#6


15  

multiprocessing.cpu_count() will return the number of logical CPUs, so if you have a quad-core CPU with hyperthreading, it will return 8. If you want the number of physical CPUs, use the python bindings to hwloc:

多进程。cpu_count()将返回逻辑CPU的数量,因此如果您有一个具有超线程的四核CPU,它将返回8。如果您想要物理cpu的数量,请使用python绑定到hwloc:

#!/usr/bin/env python
import hwloc
topology = hwloc.Topology()
topology.load()
print topology.get_nbobjs_by_type(hwloc.OBJ_CORE)

hwloc is designed to be portable across OSes and architectures.

hwloc被设计成跨os和体系结构的可移植性。

#7


7  

Can't figure out how to add to the code or reply to the message but here's support for jython that you can tack in before you give up:

不知道如何添加代码或回复消息,但这里有对jython的支持,您可以在放弃之前添加:

# jython
try:
    from java.lang import Runtime
    runtime = Runtime.getRuntime()
    res = runtime.availableProcessors()
    if res > 0:
        return res
except ImportError:
    pass

#8


3  

You can also use "joblib" for this purpose.

你也可以使用“joblib”来达到这个目的。

import joblib
print joblib.cpu_count()

This method will give you the number of cpus in the system. joblib needs to be installed though. More information on joblib can be found here https://pythonhosted.org/joblib/parallel.html

这个方法将给出系统中cpu的数量。不过joblib需要安装。关于joblib的更多信息可以在这里找到https://pythonhosted.org/joblib/parallel.html

Alternatively you can use numexpr package of python. It has lot of simple functions helpful for getting information about the system cpu.

或者,您也可以使用numexpr包的python。它有许多简单的功能,有助于获取有关系统cpu的信息。

import numexpr as ne
print ne.detect_number_of_cores()

#9


0  

Another option if you don't have Python 2.6:

如果没有Python 2.6,还有一个选项:

import commands
n = commands.getoutput("grep -c processor /proc/cpuinfo")

#10


-1  

This is the function cpu_count from multiprocessing

:}

def cpu_count():
    '''
    Returns the number of CPUs in the system
    '''
    if sys.platform == 'win32':
        try:
            num = int(os.environ['NUMBER_OF_PROCESSORS'])
        except (ValueError, KeyError):
            num = 0
    elif 'bsd' in sys.platform or sys.platform == 'darwin':
        comm = '/sbin/sysctl -n hw.ncpu'
        if sys.platform == 'darwin':
            comm = '/usr' + comm
        try:
            with os.popen(comm) as p:
                num = int(p.read())
        except ValueError:
            num = 0
    else:
        try:
            num = os.sysconf('SC_NPROCESSORS_ONLN')
        except (ValueError, OSError, AttributeError):
           num = 0

    if num >= 1:
        return num
    else:
        raise NotImplementedError('cannot determine number of cpus')

#1


595  

If you have python with a version >= 2.6 you can simply use

如果您有python版本的>= 2.6,您可以简单地使用。

import multiprocessing

multiprocessing.cpu_count()

http://docs.python.org/library/multiprocessing.html#multiprocessing.cpu_count

http://docs.python.org/library/multiprocessing.html multiprocessing.cpu_count

#2


139  

If you're interested into the number of processors available to your current process, you have to check cpuset first. Otherwise (or if cpuset is not in use), multiprocessing.cpu_count() is the way to go in Python 2.6 and newer. The following method falls back to a couple of alternative methods in older versions of Python:

如果您对当前进程可用的处理器数量感兴趣,您必须首先检查cpuset。否则(或者如果没有使用cpuset),那么multiprocess .cpu_count()是Python 2.6中更新的方法。下面的方法可以追溯到Python的旧版本中的几个替代方法:

import os
import re
import subprocess


def available_cpu_count():
    """ Number of available virtual or physical CPUs on this system, i.e.
    user/real as output by time(1) when called with an optimally scaling
    userspace-only program"""

    # cpuset
    # cpuset may restrict the number of *available* processors
    try:
        m = re.search(r'(?m)^Cpus_allowed:\s*(.*)$',
                      open('/proc/self/status').read())
        if m:
            res = bin(int(m.group(1).replace(',', ''), 16)).count('1')
            if res > 0:
                return res
    except IOError:
        pass

    # Python 2.6+
    try:
        import multiprocessing
        return multiprocessing.cpu_count()
    except (ImportError, NotImplementedError):
        pass

    # https://github.com/giampaolo/psutil
    try:
        import psutil
        return psutil.cpu_count()   # psutil.NUM_CPUS on old versions
    except (ImportError, AttributeError):
        pass

    # POSIX
    try:
        res = int(os.sysconf('SC_NPROCESSORS_ONLN'))

        if res > 0:
            return res
    except (AttributeError, ValueError):
        pass

    # Windows
    try:
        res = int(os.environ['NUMBER_OF_PROCESSORS'])

        if res > 0:
            return res
    except (KeyError, ValueError):
        pass

    # jython
    try:
        from java.lang import Runtime
        runtime = Runtime.getRuntime()
        res = runtime.availableProcessors()
        if res > 0:
            return res
    except ImportError:
        pass

    # BSD
    try:
        sysctl = subprocess.Popen(['sysctl', '-n', 'hw.ncpu'],
                                  stdout=subprocess.PIPE)
        scStdout = sysctl.communicate()[0]
        res = int(scStdout)

        if res > 0:
            return res
    except (OSError, ValueError):
        pass

    # Linux
    try:
        res = open('/proc/cpuinfo').read().count('processor\t:')

        if res > 0:
            return res
    except IOError:
        pass

    # Solaris
    try:
        pseudoDevices = os.listdir('/devices/pseudo/')
        res = 0
        for pd in pseudoDevices:
            if re.match(r'^cpuid@[0-9]+$', pd):
                res += 1

        if res > 0:
            return res
    except OSError:
        pass

    # Other UNIXes (heuristic)
    try:
        try:
            dmesg = open('/var/run/dmesg.boot').read()
        except IOError:
            dmesgProcess = subprocess.Popen(['dmesg'], stdout=subprocess.PIPE)
            dmesg = dmesgProcess.communicate()[0]

        res = 0
        while '\ncpu' + str(res) + ':' in dmesg:
            res += 1

        if res > 0:
            return res
    except OSError:
        pass

    raise Exception('Can not determine number of CPUs on this system')

#3


60  

Another option is to use the psutil library, which always turn out useful in these situations:

另一个选择是使用psutil库,它在这些情况下总是有用的:

>>> import psutil
>>> psutil.cpu_count()
2

This should work on any platform supported by psutil(Unix and Windows).

这应该适用于psutil(Unix和Windows)支持的任何平台。

Note that in some occasions multiprocessing.cpu_count may raise a NotImplementedError while psutil will be able to obtain the number of CPUs. This is simply because psutil first tries to use the same techniques used by multiprocessing and, if those fail, it also uses other techniques.

注意,在某些情况下,多处理。cpu_count可以增加一个NotImplementedError,而psutil可以获得cpu的数量。这仅仅是因为psutil首先尝试使用多处理所使用的相同技术,如果这些技术失败,它还会使用其他技术。

#4


21  

In Python 3.4+: os.cpu_count().

在Python 3.4 +:os.cpu_count()。

multiprocessing.cpu_count() is implemented in terms of this function but raises NotImplementedError if os.cpu_count() returns None ("can't determine number of CPUs").

cpu_count()是根据这个函数实现的,但如果os.cpu_count()返回None(“无法确定cpu的数量”),则不会引发NotImplementedError。

#5


18  

platform independent:

平台无关的:

psutil.cpu_count(logical=False)

psutil.cpu_count(逻辑= False)

https://github.com/giampaolo/psutil/blob/master/INSTALL.rst

https://github.com/giampaolo/psutil/blob/master/INSTALL.rst

#6


15  

multiprocessing.cpu_count() will return the number of logical CPUs, so if you have a quad-core CPU with hyperthreading, it will return 8. If you want the number of physical CPUs, use the python bindings to hwloc:

多进程。cpu_count()将返回逻辑CPU的数量,因此如果您有一个具有超线程的四核CPU,它将返回8。如果您想要物理cpu的数量,请使用python绑定到hwloc:

#!/usr/bin/env python
import hwloc
topology = hwloc.Topology()
topology.load()
print topology.get_nbobjs_by_type(hwloc.OBJ_CORE)

hwloc is designed to be portable across OSes and architectures.

hwloc被设计成跨os和体系结构的可移植性。

#7


7  

Can't figure out how to add to the code or reply to the message but here's support for jython that you can tack in before you give up:

不知道如何添加代码或回复消息,但这里有对jython的支持,您可以在放弃之前添加:

# jython
try:
    from java.lang import Runtime
    runtime = Runtime.getRuntime()
    res = runtime.availableProcessors()
    if res > 0:
        return res
except ImportError:
    pass

#8


3  

You can also use "joblib" for this purpose.

你也可以使用“joblib”来达到这个目的。

import joblib
print joblib.cpu_count()

This method will give you the number of cpus in the system. joblib needs to be installed though. More information on joblib can be found here https://pythonhosted.org/joblib/parallel.html

这个方法将给出系统中cpu的数量。不过joblib需要安装。关于joblib的更多信息可以在这里找到https://pythonhosted.org/joblib/parallel.html

Alternatively you can use numexpr package of python. It has lot of simple functions helpful for getting information about the system cpu.

或者,您也可以使用numexpr包的python。它有许多简单的功能,有助于获取有关系统cpu的信息。

import numexpr as ne
print ne.detect_number_of_cores()

#9


0  

Another option if you don't have Python 2.6:

如果没有Python 2.6,还有一个选项:

import commands
n = commands.getoutput("grep -c processor /proc/cpuinfo")

#10


-1  

This is the function cpu_count from multiprocessing

:}

def cpu_count():
    '''
    Returns the number of CPUs in the system
    '''
    if sys.platform == 'win32':
        try:
            num = int(os.environ['NUMBER_OF_PROCESSORS'])
        except (ValueError, KeyError):
            num = 0
    elif 'bsd' in sys.platform or sys.platform == 'darwin':
        comm = '/sbin/sysctl -n hw.ncpu'
        if sys.platform == 'darwin':
            comm = '/usr' + comm
        try:
            with os.popen(comm) as p:
                num = int(p.read())
        except ValueError:
            num = 0
    else:
        try:
            num = os.sysconf('SC_NPROCESSORS_ONLN')
        except (ValueError, OSError, AttributeError):
           num = 0

    if num >= 1:
        return num
    else:
        raise NotImplementedError('cannot determine number of cpus')