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 checked the sys module but it has no such functio开发者_Python百科n.
I can use an external library also if required.
The platform.processor() function returns the processor name as a string.
>>> import platform
>>> platform.processor()
'Intel64 Family 6 Model 23 Stepping 6, GenuineIntel'
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).decode().strip()
for line in all_info.split("\n"):
if "model name" in line:
return re.sub( ".*model name.*:", "", line,1)
return ""
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']
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...)
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())
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 ""
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
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'}
[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
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)
精彩评论