質問

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!

役に立ちましたか?

解決

According to http://archive.richweb.com/cpu_info, determining a cpu hyperthreading a bit complicated, but still useful.

Note that method is linux-specific.

他のヒント

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().

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.

On windows, to see if hyperthreading is enbabled you can do some WMI magic with pywin32:

Wrong idea! On my Core2Due (without any hyperthrating) it gives same output:

<on "CPU0", hyperthreading is active>

I think you must change string in code to:

if cpu.NumberOfCores < cpu.NumberOfLogicalProcessors

(no LowerOrEqual just Lower)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top