Programming

파이썬을 사용하여 CPU 수를 찾는 방법

procodes 2020. 2. 15. 23:26
반응형

파이썬을 사용하여 CPU 수를 찾는 방법


파이썬을 사용하여 로컬 컴퓨터의 CPU 수를 알고 싶습니다. 최적의 스케일링 사용자 공간 전용 프로그램으로 호출하면 결과가 user/real출력 되어야합니다 time(1).


파이썬 버전이 2.6 이상인 경우 간단히 사용할 수 있습니다.

import multiprocessing

multiprocessing.cpu_count()

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


현재 프로세스에서 사용할 수있는 프로세서 관심이있는 경우 먼저 cpuset 을 확인해야합니다 . 그렇지 않으면 (또는 cpuset을 사용하지 않는 경우) multiprocessing.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')

또 다른 옵션은 psutil라이브러리 를 사용하는 것입니다. 라이브러리는 다음 상황에서 항상 유용합니다.

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

psutil(Unix 및 Windows)가 지원하는 모든 플랫폼에서 작동합니다 .

참고 어떤 경우에 있음을 multiprocessing.cpu_count인상 할 수있다 NotImplementedError동안 psutilCPU의 수를 얻을 수있을 것입니다. 이는 단순히 psutil먼저 사용 된 것과 동일한 기술을 사용하려고 시도하고 multiprocessing실패 할 경우 다른 기술도 사용하기 때문입니다.


Python 3.4 이상에서 : os.cpu_count () .

multiprocessing.cpu_count()이 함수의 관점에서 구현되지만 NotImplementedError, os.cpu_count()리턴 되면 None( "CPU 수를 판별 할 수 없음") 발생합니다.


import os

print(os.cpu_count())

독립적 인 플랫폼 :

psutil.cpu_count (논리 = 거짓)

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


len(os.sched_getaffinity(0)) 당신이 보통 원하는 것입니다

https://docs.python.org/3/library/os.html#os.sched_getaffinity

os.sched_getaffinity(0)(Python 3에 추가됨)는 sched_setaffinityLinux 시스템 호출을 고려하여 사용 가능한 CPU 세트를 리턴합니다 . 이는 프로세스 및 해당 하위 프로세스에서 실행할 수있는 CPU를 제한합니다.

0현재 프로세스의 값을 얻는 것을 의미합니다. 이 함수는 set()허용 된 CPU를 반환 하므로에 필요합니다 len().

multiprocessing.cpu_count() 반면에 총 물리적 CPU 수만 반환합니다.

Platform LSF 와 같은 특정 클러스터 관리 시스템은 작업 CPU 사용을 제한 하기 때문에 차이점이 특히 중요합니다 sched_getaffinity.

따라서를 사용 multiprocessing.cpu_count()하면 스크립트가 사용 가능한 것보다 더 많은 코어를 사용하려고 시도 할 수 있으며 이로 인해 과부하 및 시간 초과가 발생할 수 있습니다.

taskset유틸리티 와의 선호도를 제한하여 차이를 구체적으로 확인할 수 있습니다 .

예를 들어, 16 코어 시스템에서 Python을 1 코어 (코어 0)로만 제한하는 경우 :

taskset -c 0 ./main.py

테스트 스크립트로 :

main.py

#!/usr/bin/env python3

import multiprocessing
import os

print(multiprocessing.cpu_count())
print(len(os.sched_getaffinity(0)))

출력은 다음과 같습니다.

16
1

nproc 그러나 기본적으로 선호도를 존중하고 다음을 수행합니다.

taskset -c 0 nproc

출력 :

1

그리고 man nproc그것을 매우 명백하게 만듭니다 :

사용 가능한 처리 장치 수를 인쇄

nproc--all당신이 물리적 CPU 수를 얻고 싶은 것을 덜 일반적인 경우에 대한 플래그를 :

taskset -c 0 nproc --all

이 방법의 유일한 단점은 이것이 유닉스 전용 인 것 같습니다. Windows에 유사한 유사성 API가 있어야한다고 가정 SetProcessAffinityMask했으므로 이식되지 않은 이유가 궁금합니다. 그러나 나는 Windows에 대해 아무것도 모른다.

우분투 16.04, Python 3.5.2에서 테스트되었습니다.


multiprocessing.cpu_count()논리 CPU 수를 반환하므로 하이퍼 스레딩이 포함 된 쿼드 코어 CPU가 있으면를 반환 8합니다. 실제 CPU 수를 원하는 경우 pyloc 바인딩을 사용하여 hwloc을 실행하십시오.

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

hwloc은 OS와 아키텍처에서 이식 가능하도록 설계되었습니다.


하이퍼 스레드 CPU 수를 제공합니다

  1. multiprocessing.cpu_count()
  2. os.cpu_count()

이것들은 당신에게 가상 머신 CPU 수를 제공합니다

  1. psutil.cpu_count()
  2. numexpr.detect_number_of_cores()

VM에서 작업하는 경우에만 중요합니다.


코드에 추가하거나 메시지에 회신하는 방법을 알 수 없지만 포기하기 전에 해결할 수있는 jython에 대한 지원은 다음과 같습니다.

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

이 목적으로 "joblib"를 사용할 수도 있습니다.

import joblib
print joblib.cpu_count()

이 방법은 시스템의 CPU 수를 제공합니다. joblib를 설치해야합니다. joblib에 대한 자세한 내용은 https://pythonhosted.org/joblib/parallel.html을 참조하십시오.

또는 numexpr python 패키지를 사용할 수 있습니다. 시스템 CPU에 대한 정보를 얻는 데 도움이되는 간단한 기능이 많이 있습니다.

import numexpr as ne
print ne.detect_number_of_cores()

Python 2.6이없는 경우 다른 옵션 :

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

참고 URL : https://stackoverflow.com/questions/1006289/how-to-find-out-the-number-of-cpus-using-python



반응형