Author keirlawson
Recipients keirlawson
Date 2019-02-20.16:56:22
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
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
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
Date User Action Args
2019-02-20 16:56:22keirlawsonsetrecipients: + keirlawson
2019-02-20 16:56:22keirlawsonsetmessageid: <>
2019-02-20 16:56:22keirlawsonlinkissue36054 messages
2019-02-20 16:56:22keirlawsoncreate