在 Python 中获取处理器信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4842448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting processor information in Python
提问by user225312
Using Python is there any way to find out the processor information... (I need the name)
使用Python有什么办法可以找出处理器信息......(我需要名字)
I need the name of the processor that the interpreter is running on. I checked the sys module but it has no such function.
我需要运行解释器的处理器的名称。我检查了 sys 模块,但它没有这样的功能。
I can use an external library also if required.
如果需要,我也可以使用外部库。
回答by user225312
The platform.processor()function returns the processor name as a string.
所述platform.processor()函数返回处理器名称为字符串。
>>> import platform
>>> platform.processor()
'Intel64 Family 6 Model 23 Stepping 6, GenuineIntel'
回答by Spacedman
There's some code here:
这里有一些代码:
https://github.com/pydata/numexpr/blob/master/numexpr/cpuinfo.py
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.
它非常依赖操作系统,所以有很多 if 分支。但它确实可以解决所有 CPU 功能。
$ 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.
对于 linux,它在 /proc/cpuinfo 中查找并尝试使用 uname。对于 Windows,它看起来像是使用注册表。
To get the [first] processor name using this module:
要使用此模块获取 [first] 处理器名称:
>>> 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...)
如果它有多个处理器,那么 cpuinfo.cpu.info 的元素将有它们的名字。不过,我认为我从未见过配备两种不同处理器的 PC(自从 80 年代以来,您可以为 6502 CPU BBC Micro 配备 Z80 协处理器……)
回答by dbn
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 ""
回答by Phil L.
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 ""
回答by oche
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.
Windows 的 if-cases ie platform.processor() 只给出处理器的描述或系列名称,例如 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
获得与 Darwin 和 Linux 的 if-blocks 输出相同的实际 CPU 模型,例如 Intel(R) Core(TM) i7-4790K CPU @ 4.00GHz Intel64 Family 6 Model 60 Stepping 3, GenuineIntel
回答by anatoly techtonik
Looks like the missing script in @Spacedman answer is here:
看起来@Spacedman 答案中缺少的脚本在这里:
https://github.com/pydata/numexpr/blob/master/numexpr/cpuinfo.py
https://github.com/pydata/numexpr/blob/master/numexpr/cpuinfo.py
It is patched to work with Python 3.
它已修补以与 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:
数据的结构确实依赖于操作系统,在 Windows 上它看起来像这样:
>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:
在 Linux 上则不同:
# 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'}
回答by cardamom
I tried out various solutions here. cat /proc/cpuinfgives 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:
我在这里尝试了各种解决方案。cat /proc/cpuinf为多核机器提供大量输出,很多页长,platform.processor()似乎给你很少。使用 Linux 和 Python 3,以下返回了大约 20 行的非常有用的摘要:
import subprocess
print((subprocess.check_output("lscpu", shell=True).strip()).decode())
回答by serv-inc
回答by Joshua Detwiler
For Linux, and backwards compatibility with Python (not everyone has cpuinfo), you can parse through /proc/cpuinfodirectly. To get the processor speed, try:
对于Linux,并且向后兼容Python(不是每个人都有cpuinfo),可以/proc/cpuinfo直接解析。要获得处理器速度,请尝试:
# Take any float trailing "MHz", some whitespace, and a colon.
speeds = re.search("MHz\s*: (\d+\.?\d*)", cpuinfo_content)
Note the necessary use of \sfor whitespace.../proc/cpuinfoactually has tab characters and I toiled for hours working with seduntil I came up with:
注意\s空格的必要使用.../proc/cpuinfo实际上有制表符,我辛苦工作了几个小时,sed直到我想出:
sed -rn 's/cpu MHz[ \t]*: ([0-9]+\.?[0-9]*)//gp' /proc/cpuinfo
I lacked the \tand it drove me mad because I either matched the whole file or nothing.
我缺少\t,这让我很生气,因为我要么匹配整个文件,要么什么都不匹配。
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)

