[New-bugs-announce] [issue36054] Way to detect CPU count inside docker container

Keir Lawson report at bugs.python.org
Wed Feb 20 11:56:22 EST 2019


New submission from Keir Lawson <keirlawson at gmail.com>:

There appears to be no way to detect the number of CPUs allotted to a Python program within a docker container.  With the following script:

import os

print("os.cpu_count(): " + str(os.cpu_count()))
print("len(os.sched_getaffinity(0)): " + str(len(os.sched_getaffinity(0))))

when run in a container (from an Ubuntu 18.04 host) I get:

docker run -v "$PWD":/src/ -w /src/ --cpus=1 python:3.7 python detect_cpus.py
os.cpu_count(): 4
len(os.sched_getaffinity(0)): 4

Recent vesions of Java are able to correctly detect the CPU allocation:

docker run -it --cpus 1 openjdk:10-jdk
Feb 20, 2019 4:20:29 PM java.util.prefs.FileSystemPreferences$1 run
INFO: Created user preferences directory.
|  Welcome to JShell -- Version 10.0.2
|  For an introduction type: /help intro

jshell> Runtime.getRuntime().availableProcessors()
$1 ==> 1

----------
components: Library (Lib)
messages: 336117
nosy: keirlawson
priority: normal
severity: normal
status: open
title: Way to detect CPU count inside docker container
type: performance
versions: Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue36054>
_______________________________________


More information about the New-bugs-announce mailing list