gpt4 book ai didi

python - 如果在 python multiprocessing.cpu_count() 中返回 64,我是否受益于 gcloud Compute Engine 的 96 个 vCPU?

转载 作者:行者123 更新时间:2023-11-28 19:03:01 26 4
gpt4 key购买 nike

我在 Google Compute Engine 上运行 Python 并行 CPU 密集型任务。因此,我可以运行它的 vCPU 数量越多,速度就越快。

我读到创建一个大小大于可用 vCPU 数量的多处理池没有意义,这是有道理的,所以我确定了我的 multiprocessing.dummy.Pool 的大小使用 multiprocessing.cpu_count() 的池。

我在 Pod 中运行此脚本,使用 gcloud Kubernetes Engine,并在开发期间在 vCPU 少于 96 的机器上进行了测试。自动确定的池大小似乎总是与 vCPU 的数量相匹配。但是,在具有 96 个 vCPU 的机器上运行它,multiprocessing.cpu_count() 返回 64 而不是 96。我不在乎手动将该大小设置为 96,但问题是,我会从中受益吗额外的 32 个 vCPU 如果 python 没有“意识到”它们?

该机器是运行 Container-Optimized OS (cos) 的 n1-highcpu-96(96 个 vCPU,86.4 GB 内存)。 Python 版本为 3.6.3。

最佳答案

留言板上有一个答案,有人在对该问题的评论中链接到该答案,但是,最好在此页面上找到答案以及一些解释。

简短的回答:在 pod 内,运行 grep -c ^processor/proc/cpuinfo - 这个数字应该与 multiprocessing.cpu_count() 一致。如果是这样,您可以信任 multiprocessing.cpu_count()

但是,AFAICT,这会识别节点上的所有核心,并完全忽略在 Kubernetes 部署 YAML 中设置的资源限制。例如,您的部署文件可能包含:

spec:
containers:
- image: IMAGENAME
name: LABEL
ports:
- containerPort: 5000
resources:
limits:
cpu: 100m
memory: 400M
requests:
cpu: 50m
memory: 200M

this article ,作者给出了以下函数,它尊重资源限制(不是请求):

import math
from pathlib import Path


def get_cpu_quota_within_docker():
cpu_cores = None

cfs_period = Path("/sys/fs/cgroup/cpu/cpu.cfs_period_us")
cfs_quota = Path("/sys/fs/cgroup/cpu/cpu.cfs_quota_us")

if cfs_period.exists() and cfs_quota.exists():
# we are in a linux container with cpu quotas!
with cfs_period.open('rb') as p, cfs_quota.open('rb') as q:
p, q = int(p.read()), int(q.read())

# get the cores allocated by dividing the quota
# in microseconds by the period in microseconds
cpu_cores = math.ceil(q / p) if q > 0 and p > 0 else None

return cpu_cores

因此,对于 YAML 示例,除法产生 0.1,但调用 ceil 的 b/c 返回 1.0。因此,您可能正在寻找类似以下内容的内容(假设您定义了上面定义的函数 get_cpu_quota_within_docker):

import multiprocessing

from somewhere import get_cpu_quota_within_docker

docker_cpus = get_cpu_quota_within_docker()
cpu_count = docker_cpus if docker_cpus else multiprocessing.cpu_count()

关于python - 如果在 python multiprocessing.cpu_count() 中返回 64,我是否受益于 gcloud Compute Engine 的 96 个 vCPU?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50464507/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com