My computers CPU has 4 cores and 2 threads for each core, as I can get it by lscpu
command:
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
Address sizes: 48 bits physical, 48 bits virtual
CPU(s): 4
On-line CPU(s) list: 0-3
Thread(s) per core: 2
...
I can get the numbers of cores by multiprocessing.cpu_count()
python standard. But I can't find any way to get the number of threads per core by python. I know I can execute lscpu
command and get the number of threads by parsing the output in python. But is there any standard solution?
You can use psutil
module in python3.
python3 -m pip install psutil
The psutil.cpu_count
method returns the number of logical CPUs in the system (same as os.cpu_count
in Python 3.4) or None
if undetermined. Logical cores means the number of physical cores multiplied by the number of threads that can run on each core (this is known as Hyper Threading). If logical
is False
return the number of physical cores only (Hyper Thread CPUs are excluded) or None
if undetermined.
import psutil
threads_count = psutil.cpu_count() / psutil.cpu_count(logical=False)