Python multiprocessing.cpu_count() 在 4 核 Nvidia Jetson TK1 上返回“1”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31344582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:50:44  来源:igfitidea点击:

Python multiprocessing.cpu_count() returns '1' on 4-core Nvidia Jetson TK1

pythoncpupython-multiprocessing

提问by Hephaestus

Can anyone tell me why Python's multiprocessing.cpu_count()function would return 1when when called on a Jetson TK1 with four ARMv7 processors?

谁能告诉我为什么在带有四个 ARMv7 处理器的 Jetson TK1 上调用Python 的multiprocessing.cpu_count()函数时会返回1

>>> import multiprocessing
>>> multiprocessing.cpu_count()
1

The Jetson TK1 board is more or less straight out of the box, and no one has messed with cpusets. From within the same Python shell I can print the contents of /proc/self/statusand it tells me that the process should have access to all four cores:

Jetson TK1 开发板或多或少是直接开箱即用的,没有人弄乱过 cpuset。在同一个 Python shell 中,我可以打印其中的内容,/proc/self/status它告诉我该进程应该可以访问所有四个核心:

>>> print open('/proc/self/status').read()
----- (snip) -----
Cpus_allowed:   f
Cpus_allowed_list:      0-3
----- (snip) -----

What else could be causing this behavior from cpu_count()?

还有什么可能导致这种行为cpu_count()

Edit:

编辑:

To test Klaus's hypothesis, I used the following code to run a very simple experiment:

为了检验 Klaus 的假设,我使用以下代码运行了一个非常简单的实验:

import multiprocessing

def f(x):
    n = 0
    for i in xrange(10000):
        n = max(n, multiprocessing.cpu_count())
    return n

p = multiprocessing.Pool(5)
for i in range(10):
    print p.map(f, [1,2,3,4,5])

Which produced the following output:

这产生了以下输出:

[3, 3, 3, 3, 1]
[4, 3, 3, 3, 3]
[4, 3, 3, 3, 3]
[3, 3, 4, 3, 3]
[4, 3, 3, 3, 3]
[3, 3, 4, 3, 3]
[4, 3, 3, 3, 3]
[3, 3, 4, 3, 3]
[3, 3, 3, 4, 3]
[4, 3, 3, 3, 3]

Running just a single iteration of p.map(f, [1,2,3,4,5])usually produces [1, 1, 1, 1, 1], although occasionally a 2will appear as one of the list elements.

只运行一次迭代p.map(f, [1,2,3,4,5])通常会产生[1, 1, 1, 1, 1],尽管偶尔 a2会作为列表元素之一出现。

采纳答案by Klaus D.

On Linux systems multiprocessing.cpu_count()relies on a sysconf (_SC_NPROCESSORS_ONLN)call, which returns the number of onlineCPUs in contrast to sysconf (_SC_NPROCESSORS_CONF)which returns the number of configuredCPUs.

在 Linux 系统上multiprocessing.cpu_count()依赖于sysconf (_SC_NPROCESSORS_ONLN)调用,该调用返回在线CPU的数量,而sysconf (_SC_NPROCESSORS_CONF)后者返回已配置的CPU 的数量。

The values might differ in systems with advanced CPU power management functionality that sets CPU cores offline to save energy or with similar dynamic CPU activation functionality.

在具有高级 CPU 电源管理功能(将 CPU 内核设置为离线以节省能源)或具有类似动态 CPU 激活功能的系统中,这些值可能会有所不同。

回答by Rizzer

The documentation for os.cpu_count() (which declares that it returns the total number of CPUS, not the number of usable CPUs) provides a means to count usable CPUs:

os.cpu_count() 的文档(声明它返回 CPU 总数,而不是可用 CPU 的数量)提供了一种计算可用 CPU 的方法:

len(os.sched_getaffinity(0))

see https://docs.python.org/3/library/os.html#os.cpu_count

https://docs.python.org/3/library/os.html#os.cpu_count