Per this question and answer -- Python multiprocessing.cpu_count() returns '1' on 4-core Nvidia Jetson TK1 -- the output of Python\'s multiprocessing.cpu_count()<
I don't think you will get any truly portable answers, so I will give a correct one.
The correct* answer for Linux is len(os.sched_getaffinity(pid))
, where pid
may be 0
for the current process. This function is exposed in Python 3.3 and later; if you need it in earlier, you'll have to do some fancy cffi
coding.
Edit: you might try to see if you can use a function int omp_get_num_procs();
if it exists, it is the only meaningful answer I found on this question but I haven't tried it from Python.
Use psutil:
from the doc https://psutil.readthedocs.io/en/latest/:
>>> import psutil
>>> psutil.cpu_count()
4
>>> psutil.cpu_count(logical=False) # Ignoring virtual cores
2
This is portable