Getting processor information in Python

后端 未结 10 1924
一向
一向 2020-11-29 04:20

Using Python is there any way to find out the processor information... (I need the name)

I need the name of the processor that the interpreter is running on. I check

相关标签:
10条回答
  • 2020-11-29 04:58

    Here's a hackish bit of code that should consistently find the name of the processor on the three platforms that I have any reasonable experience.

    import os, platform, subprocess, re
    
    def get_processor_name():
        if platform.system() == "Windows":
            return platform.processor()
        elif platform.system() == "Darwin":
            os.environ['PATH'] = os.environ['PATH'] + os.pathsep + '/usr/sbin'
            command ="sysctl -n machdep.cpu.brand_string"
            return subprocess.check_output(command).strip()
        elif platform.system() == "Linux":
            command = "cat /proc/cpuinfo"
            all_info = subprocess.check_output(command, shell=True).strip()
            for line in all_info.split("\n"):
                if "model name" in line:
                    return re.sub( ".*model name.*:", "", line,1)
        return ""
    
    0 讨论(0)
  • 2020-11-29 05:02

    I tried out various solutions here. cat /proc/cpuinf gives a huge amount of output for a multicore machine, many pages long, platform.processor() seems to give you very little. Using Linux and Python 3, the following returns quite a useful summary of about twenty lines:

    import subprocess
    print((subprocess.check_output("lscpu", shell=True).strip()).decode())
    
    0 讨论(0)
  • 2020-11-29 05:05

    For an easy to use package, you can use cpuinfo.

    Install as pip install py-cpuinfo

    Use from the commandline: python -m cpuinfo

    Code:

    import cpuinfo
    cpuinfo.get_cpu_info()['brand']
    
    0 讨论(0)
  • 2020-11-29 05:07

    There's some code here:

    https://github.com/pydata/numexpr/blob/master/numexpr/cpuinfo.py

    its very OS-dependent, so there's lots of if-branches. But it does work out all the CPU capabilities.

    $ python cpuinfo.py 
    CPU information: getNCPUs=2 has_mmx has_sse has_sse2 is_64bit is_Intel is_Pentium is_PentiumIV
    

    For linux it looks in /proc/cpuinfo and tries using uname. For Windows it looks like it uses the registry.

    To get the [first] processor name using this module:

    >>> import cpuinfo
    >>> cpuinfo.cpu.info[0]['model name']
    'Intel(R) Pentium(R) 4 CPU 3.60GHz'
    

    If its got more than one processor, then the elements of cpuinfo.cpu.info will have their names. I don't think I've ever seen a PC with two different processors though (not since the 80's when you could get a Z80 co-processor for your 6502 CPU BBC Micro...)

    0 讨论(0)
  • 2020-11-29 05:09

    Working code (let me know if this doesn't work for you):

    import platform, subprocess
    
    def get_processor_info():
        if platform.system() == "Windows":
            return platform.processor()
        elif platform.system() == "Darwin":
            return subprocess.check_output(['/usr/sbin/sysctl', "-n", "machdep.cpu.brand_string"]).strip()
        elif platform.system() == "Linux":
            command = "cat /proc/cpuinfo"
            return subprocess.check_output(command, shell=True).strip()
        return ""
    
    0 讨论(0)
  • 2020-11-29 05:10

    Looks like the missing script in @Spacedman answer is here:

    https://github.com/pydata/numexpr/blob/master/numexpr/cpuinfo.py

    It is patched to work with Python 3.

    >python cpuinfo.py
    CPU information: CPUInfoBase__get_nbits=32 getNCPUs=2 has_mmx is_32bit is_Intel is_i686
    

    The structure of data is indeed OS-dependent, on Windows it looks like this:

    >python -c "import cpuinfo, pprint; pprint.pprint(cpuinfo.cpu.info[0])"
    {'Component Information': '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00',
     'Configuration Data': '\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00',
     'Family': 6,
     'FeatureSet': 2687451135L,
     'Identifier': u'x86 Family 6 Model 23 Stepping 10',
     'Model': 23,
     'Platform ID': 128,
     'Previous Update Signature': '\x00\x00\x00\x00\x0c\n\x00\x00',
     'Processor': '0',
     'ProcessorNameString': u'Intel(R) Core(TM)2 Duo CPU     P8600  @ 2.40GHz',
     'Stepping': 10,
     'Update Signature': '\x00\x00\x00\x00\x0c\n\x00\x00',
     'Update Status': 2,
     'VendorIdentifier': u'GenuineIntel',
     '~MHz': 2394}
    

    On Linux it is different:

    # python -c "import cpuinfo, pprint; pprint.pprint(cpuinfo.cpu.info[0])"
    {'address sizes': '36 bits physical, 48 bits virtual',
     'apicid': '0',
     'bogomips': '6424.11',
     'bugs': '',
     'cache size': '2048 KB',
     'cache_alignment': '128',
     'clflush size': '64',
     'core id': '0',
     'cpu MHz': '2800.000',
     'cpu cores': '2',
     'cpu family': '15',
     'cpuid level': '6',
     'flags': 'fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx lm const
    ant_tsc pebs bts nopl pni dtes64 monitor ds_cpl est cid cx16 xtpr pdcm lahf_lm',
     'fpu': 'yes',
     'fpu_exception': 'yes',
     'initial apicid': '0',
     'microcode': '0xb',
     'model': '6',
     'model name': 'Intel(R) Pentium(R) D CPU 3.20GHz',
     'physical id': '0',
     'power management': '',
     'processor': '0',
     'siblings': '2',
     'stepping': '5',
     'uname_m': 'x86_64',
     'vendor_id': 'GenuineIntel',
     'wp': 'yes'}
    
    0 讨论(0)
提交回复
热议问题