在Python中检测*可用* CPU数量的可移植方式 [英] Portable way of detecting number of *usable* CPUs in Python

查看:79
本文介绍了在Python中检测*可用* CPU数量的可移植方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题和答案- Python multiprocessing.cpu_count()在4核Nvidia Jetson TK1上返回'1'-某些系统上Python的 multiprocessing.cpu_count()函数的输出反映了正在有效使用 的CPU数量,而不是数量调用的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.

常见的Python习惯用法是使用的返回值cpu_count()初始化 Pool 中的进程数。但是,在使用这种动态CPU激活策略的系统上,该惯用语很难破解(至少在相对静态的系统上)。

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

是否有一些简单(可移植)的方法来获取可用处理器的数量(与当前使用的数量相反) )来自Python?

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

注意:


  1. 如何使用python 找出CPU的数量,因为正如 this 问题顶部的问题所指出的那样,打印 / proc / self / status 显示该程序可用的所有4个内核。

  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.

在我看来,便携式不包括任何涉及解析 / proc / self / status 内容的方法,其格式可能因Linux版本而异,甚至在OS X上都不存在(其他伪文件也是如此。)

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


推荐答案

我认为您不会得到任何真正可移植的答案,所以我会给出正确的答案。

I don't think you will get any truly portable answers, so I will give a correct one.

正确的 * 答案Linux是 len(os.sched_getaffinity(pid)),其中 pid 可能是 0 用于当前进程。该函数在Python 3.3及更高版本中公开;如果您需要更早的版本,则必须进行一些精美的 cffi 编码。

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.

编辑:您可以尝试看看是否可以使用函数 int omp_get_num_procs(); (如果存在),这是我在,但我尚未从Python中尝试过。

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.

这篇关于在Python中检测*可用* CPU数量的可移植方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆