在Docker容器中使用所有可用CPU的方法比较尴尬:

我使用以下可怕的语法来告诉我的docker容器使用主机上的所有CPU:

docker run --cpuset-cpus="0-`python3 -c "from multiprocessing import cpu_count; print(cpu_count() -1)"`" ubuntu:latest /bin/bash 

有没有更好的办法?

您可以使用nproc来返回CPU核心的数量。

为了得到CPU内核的数量 – 1,POSIX shell中的算术用$和双括号完成

 docker run --cpuset-cpus="0-$(($(nproc)-1))" ubuntu:latest echo "hi"