* * Available the CPU detects the number of the mobile manner in Python

According to this question and answer - Python multiprocessing.cpu_count () returns the Jetson TK1 4-core Nvidia '1' - Python multiprocessing.cpu_count()on some systems the functions of output reflects the initiative to use the number of CPU , rather than the number of CPU actually can pass call Python programs.

A common Python usage is a return-value cpu_count()to initialize a number of processes Pool. However, on systems that use this "dynamic CPU active" policy, the idiom is very serious (at least in the relatively static system).

Are there some direct (and portable) way to get Python available number of processors (and currently used opposite number )?

notes:

  1. The problem is not accepted answer to the question how to find the number of python CPU use , because hanging above a problem, explain the problem, print the contents of the /proc/self/statusdisplay of all four cores available for the program.

  2. In my opinion, "portable" excludes any method involves parsing the content /proc/self/status, the format may be due to the release version of Linux-specific, and do not even exist on the OS X. (For any other pseudo-file as well, the same.)

 

solution


I do not think you will get any truly portable answer, so I will give a correct answer.

Right * for Linux is the answer len(os.sched_getaffinity(pid)), where is pidlikely to be 0the current process. This function is disclosed in Python 3.3 and later; if you need it before, you will have to do some fancy cfficoding.

Edit: You might try to see if you can use the function ( int omp_get_num_procs();if present), this is my problem only meaningful answers to be found on, but I have not tried it from Python.

This article first appeared in Python black hole net , blog sync with the new park

 

Guess you like

Origin www.cnblogs.com/pythonzhichan/p/11444502.html