有人可以告诉我为什么Python的multiprocessing.cpu_count()
函数在使用四个ARMv7处理器调用Jetson TK1时会返回1
吗?
>>> import multiprocessing
>>> multiprocessing.cpu_count()
1
Jetson TK1开发板或多或少是开箱即用的,并没有人与cpusets混淆。在同一个Python shell中,我可以打印/proc/self/status
的内容,它告诉我该进程应该可以访问所有四个核心:
>>> print open('/proc/self/status').read()
----- (snip) -----
Cpus_allowed: f
Cpus_allowed_list: 0-3
----- (snip) -----
cpu_count()
可能导致此行为的其他原因是什么?
为了测试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])
产生了以下输出:
[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]
仅运行p.map(f, [1,2,3,4,5])
的单次迭代通常会生成[1, 1, 1, 1, 1]
,但有时候2
会显示为列表元素之一。
答案 0 :(得分:19)
在Linux系统上user
id_user | name | address | id_parent
依赖于multiprocessing.cpu_count()
调用,该调用返回在线 CPU的数量,与sysconf (_SC_NPROCESSORS_ONLN)
相反,后者返回已配置 CPU。
具有高级CPU电源管理功能的系统中的值可能不同,这些功能可将CPU核心设置为脱机以节省能源或具有类似的动态CPU激活功能。
答案 1 :(得分:0)
os.cpu_count()的文档(声明其返回CPUS的总数,而不是可用CPU的数量)提供了一种计算可用CPU的方法:
len(os.sched_getaffinity(0))