![](/img/trans.png)
[英]Python multiprocessing.cpu_count() returns '1' on 4-core Nvidia Jetson TK1
[英]Do I benefit from the 96 vCPUs of a gcloud Compute Engine if in python multiprocessing.cpu_count() returns 64?
我在Google Compute Engine上運行python並行CPU密集型任務。 因此,我可以運行它的vCPU數量越多,速度就越快。
我已經讀過創建一個大小超過可用vCPU數量的多處理池沒有意義,這是有意義的,所以我使用multiprocessing.cpu_count()
確定multiprocessing.dummy.Pool
池的大小。
我使用gcloud Kubernetes Engine在Pod中運行此腳本,並在開發期間在少於96個vCPU的計算機上進行測試。 自動確定的池大小似乎總是與vCPU的數量相匹配。 但是,在具有96個vCPU的計算機上運行它, multiprocessing.cpu_count()
返回64而不是96.我不在乎手動將該大小設置為96但問題是,如果python不是,我將從這些額外的32個vCPU中受益“意識到”他們?
該機器是運行容器優化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
在本文中 ,作者給出了以下功能,它尊重資源限制 (而不是請求):
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.