I have Intel i7-2600K quadcore, with hyperthreading enabled on Ubuntu 12.04. I know that I can find out how many cores I have in Python with import multiprocessing; multiprocessing.cpu_count()
, but that gives me 8 because I have hyperthreading enabled on 4 physical cores. I'm rather interested in finding out how many physical cores I have. Is there a way to do that in Python? Alternatively, is there a way of finding out in Python whether hyperthreading is enabled? Thank you in advance for your help!
To get hyperthreading information on the mac, you can use:
os.popen('sysctl hw').readlines()[1:20]
and compare the 'hw.activecpu'
value to the 'hw.physicalcpu'
value, the former being the number of cpus including hyperthreading. Or you could just compare 'hw.physicalcpu'
with the value returned from multiprocessing.cpu_count()
.
On linux you could do something similar using:
os.popen('lscpu').readlines()
and multiply the 'Socket(s)'
value and the 'Core(s) per socket'
value to get the number of physical cpus. Again, you could check this number against multiprocessing.cpu_count()
.
I dont use Windows, so I cant help you there, but it seems like others have some ideas in this direction
An example related to this can be found here hardware_info().
According to http://archive.richweb.com/cpu_info, determining a cpu hyperthreading a bit complicated, but still useful.
Note that method is linux-specific.
On windows, to see if hyperthreading is enbabled you can do some WMI magic with pywin32:
from __future__ import print_function
from win32com.client import GetObject
winmgmts_root = GetObject("winmgmts:root\cimv2")
cpus = winmgmts_root.ExecQuery("Select * from Win32_Processor")
for cpu in cpus:
print('on "{}", hyperthreading is '.format(cpu.DeviceID), end='')
if cpu.NumberOfCores < cpu.NumberOfLogicalProcessors:
print('active')
else:
print('inactive')
On my machine, the output is:
on "CPU0", hyperthreading is active
See more info at msdn describing what can be fetched from the Win32_Processor class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With