Getting processor information in Python

后端 未结 10 1925
一向
一向 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 05:12

    [Answer]: this works the best

     import cpuinfo
     cpuinfo.get_cpu_info()['brand_raw'] # get only the brand name
    

    or

     import cpuinfo
     cpuinfo.get_cpu_info()
    

    To get all info about the cpu

    {'python_version': '3.7.6.final.0 (64 bit)',
     'cpuinfo_version': [7, 0, 0],
     'cpuinfo_version_string': '7.0.0',
     'arch': 'X86_64',
     'bits': 64,
     'count': 2,
     'arch_string_raw': 'x86_64',
     'vendor_id_raw': 'GenuineIntel',
     'brand_raw': 'Intel(R) Xeon(R) CPU @ 2.00GHz',
     'hz_advertised_friendly': '2.0000 GHz',
     'hz_actual_friendly': '2.0002 GHz',
     'hz_advertised': [2000000000, 0],
     'hz_actual': [2000176000, 0],
     'stepping': 3,
     'model': 85,
     'family': 6,
     'flags': ['3dnowprefetch',
      'abm',
      'adx', ...more
    
    0 讨论(0)
  • 2020-11-29 05:15

    For Linux, and backwards compatibility with Python (not everyone has cpuinfo), you can parse through /proc/cpuinfo directly. To get the processor speed, try:

    # Take any float trailing "MHz", some whitespace, and a colon.
    speeds = re.search("MHz\s*: (\d+\.?\d*)", cpuinfo_content)
    

    Note the necessary use of \s for whitespace.../proc/cpuinfo actually has tab characters and I toiled for hours working with sed until I came up with:

    sed -rn 's/cpu MHz[ \t]*: ([0-9]+\.?[0-9]*)/\1/gp' /proc/cpuinfo
    

    I lacked the \t and it drove me mad because I either matched the whole file or nothing.

    Try similar regular expressions for the other fields you need:

    # Take any string after the specified field name and colon.
    re.search("field name\s*: (.+)", cpuinfo_content)  
    
    0 讨论(0)
  • 2020-11-29 05:21

    The platform.processor() function returns the processor name as a string.

    >>> import platform
    >>> platform.processor()
    'Intel64 Family 6 Model 23 Stepping 6, GenuineIntel'
    
    0 讨论(0)
  • 2020-11-29 05:21

    The if-cases for Windows i.e platform.processor() just gives the description or family name of the processor e.g. Intel64 Family 6 Model 60 Stepping 3.

    I used:

      if platform.system() == "Windows":
            family = platform.processor()
            name = subprocess.check_output(["wmic","cpu","get", "name"]).strip().split("\n")[1]
            return ' '.join([name, family])
    

    to get the actual cpu model which is the the same output as the if-blocks for Darwin and Linux, e.g. Intel(R) Core(TM) i7-4790K CPU @ 4.00GHz Intel64 Family 6 Model 60 Stepping 3, GenuineIntel

    0 讨论(0)
提交回复
热议问题