Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Portable way of detecting number of *usable* CPUs in Python

Tags:

python

cpu

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() function on certain systems reflects the number of CPUs actively in use, as opposed to the number of CPUs actually usable by the calling Python program.

A common Python idiom is to use the return-value of cpu_count() to initialize the number of processes in a Pool. However, on systems that use such a "dynamic CPU activation" strategy, that idiom breaks rather badly (at least on a relatively quiescent system).

Is there some straightforward (and portable) way to get at the number of usable processors (as opposed the number currently in use) from Python?

Notes:

  1. This question is not answered by the accepted answer to How to find out the number of CPUs using python, since as noted in the question linked at the top of this question, printing the contents of /proc/self/status shows all 4 cores as being available to the program.

  2. To my mind, "portable" excludes any approach that involves parsing the contents of /proc/self/status, whose format may vary from release to release of Linux, and which doesn`t even exist on OS X. (The same goes for any other pseudo-file, as well.)

like image 710
Hephaestus Avatar asked Jul 10 '15 17:07

Hephaestus


Video Answer


2 Answers

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.

like image 141
o11c Avatar answered Sep 29 '22 03:09

o11c


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

like image 22
ninjaconcombre Avatar answered Sep 29 '22 01:09

ninjaconcombre