Docker环境下获取cpu核心数
Python代码
def docker_cpu_count():
base_cpus = "/sys/fs/cgroup/cpu"
with open(os.path.join(base_cpus, "cpu.cfs_quota_us"), 'r') as quota_fd:
cfs_quota_us = int(quota_fd.read())
with open(os.path.join(base_cpus, "cpu.cfs_period_us"), 'r') as period_fd:
cfs_period_us = int(period_fd.read())
if cfs_quota_us > -1 and cfs_period_us > 0:
return math.ceil(cfs_quota_us / cfs_period_us)
return multiprocessing.cpu_count()
参考代码:https://github.com/uilianries/conan/blob/00c232bce871d662f77a10be5729f4010a62a6e9/conans/client/tools/oss.py
发表回复