docker 容器中 os.cpu_count() , multiprocessing.cpu_count() 都为物理机cpu 数

发布时间 2023-06-01 20:26:48作者: michaelchengjl

docker 容器中 os.cpu_count() , multiprocessing.cpu_count() 都为物理机cpu 数

不论起容器的时候 是否加 --cpus 1 --cpuset-cpus 0
docker 容器中 os.cpu_count() , multiprocessing.cpu_count() 都为物理机cpu 数

# native
$ python -c 'import multiprocessing as mp; print(mp.cpu_count())'
12
# docker
$ docker run -it --rm --cpus 1 --cpuset-cpus 0 python python -c 'import multiprocessing as mp; print(mp.cpu_count())'
12

From https://docs.docker.com/config/containers/resource_constraints/#cpu - "By default, each container’s access to the host machine’s CPU cycles is unlimited." But you can limit containers with options like --cpus or --cpuset-cpus.

--cpus can be a floating point number up to the number of physical CPU's available. You can think of this number as a numerator in the fraction <--cpus arg>/<physical CPU's>. If you have 8 physical CPU's and you specify --cpus 4, what you're telling docker is to use no more than 50% (4/8) of your total CPU's. --cpus 1.5 would use 18.75% (1.5/8).

--cpuset-cpus actually does limit specifically which physical/virtual CPU's to use.

(And there are many other CPU-related options that are covered in docker's documentation.)

https://stackoverflow.com/questions/47989418/multiprocessing-python-program-inside-docker