<p>I am running a python parallel CPU intensive task on Google Compute Engine. Hence, the greater the number of vCPUs I can run it on, the greater the speed.</p>

<p>I've read that there is no point in creating a multiprocessing pool with greater size than the number of available vCPUs, which makes sense, so I determine the size of my <code>multiprocessing.dummy.Pool</code> pool using <code>multiprocessing.cpu_count()</code>.</p>

<p>I am running this script in a Pod, using gcloud Kubernetes Engine, and tested on machines with less than 96 vCPUs during development. The pool size automatically determined seemed always to match to the number of vCPUs. However, running it on a machine with 96 vCPUs, <code>multiprocessing.cpu_count()</code> returns 64 and not 96. I don't care setting that size manually to 96 but the question is, will I benefit from those extra 32 vCPUs if python is not "aware" of them?</p>

<p>The machine is a n1-highcpu-96 (96 vCPUs, 86.4 GB memory) running the Container-Optimized OS (cos). Python version is 3.6.3.</p>

xEc
<p>There is an answer in the message board that someone linked to in a comment to the question, however, it seems better to have the answer on this page, as well as some explanation.</p>

<p>The short answer: inside a pod, run <code>grep -c ^processor /proc/cpuinfo</code> - this number should agree with <code>multiprocessing.cpu_count()</code>. If it does, you can trust <code>multiprocessing.cpu_count()</code>. </p>

<p>However, AFAICT, this identifies all the cores on the node and completely ignores the resource limits set in your Kubernetes deployment YAML. For example, your deployment file might contain:</p>

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

<p>In <a href="http://blog.tabanpour.info/projects/2018/09/07/tf-docker-kube.html" rel="nofollow noreferrer">this article</a>, the author gives the following function, which respects the resource <em>limits</em> (not requests):</p>

<pre><code>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 &gt; 0 and p &gt; 0 else None

    return cpu_cores
</code></pre>

<p>So, for the example YAML, the division yields <code>0.1</code>, but b/c of the call to <code>ceil</code>, it returns <code>1.0</code>. So what you may be looking for is something like the following (assuming that you have the above-defined function <code>get_cpu_quota_within_docker</code> defined):</p>

<pre><code>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()
</code></pre>
