<p>I'm running a <code>flask</code> application with <code>gunicorn</code> and <code>gevent</code> worker class. In my own test environment, I follow the official guide <code>multiprocessing.cpu_count() * 2 + 1</code> to set worker number.</p>
<p>If I want to put the application on Kubernetes' pod and assume that resources will be like</p>
<pre><code>resources:
  limits:
    cpu: &quot;10&quot;
    memory: &quot;5Gi&quot;
  requests:
    CPU: &quot;3&quot;
    memory: &quot;3Gi&quot;
</code></pre>
<p>how to calculate the worker number? should I use limits CPU or requests CPU?</p>
<hr />
<p>PS. I'm launching application via binary file packaged by <code>pyinstaller</code>, in essence <code>flask run(python script.py)</code>, and launch gunicorn in the main thread:</p>
<pre><code>def run():
    ...
    if config.RUN_MODEL == 'GUNICORN':
        sys.argv += [
            &quot;--worker-class&quot;, &quot;event&quot;,
            &quot;-w&quot;, config.GUNICORN_WORKER_NUMBER,
            &quot;--worker-connections&quot;, config.GUNICORN_WORKER_CONNECTIONS,
            &quot;--access-logfile&quot;, &quot;-&quot;,
            &quot;--error-logfile&quot;, &quot;-&quot;,
            &quot;-b&quot;, &quot;0.0.0.0:8001&quot;,
            &quot;--max-requests&quot;, config.GUNICORN_MAX_REQUESTS,
            &quot;--max-requests-jitter&quot;, config.GUNICORN_MAX_REQUESTS_JITTER,
            &quot;--timeout&quot;, config.GUNICORN_TIMEOUT,
            &quot;--access-logformat&quot;, '%(t)s %(l)s %(u)s &quot;%(r)s&quot; %(s)s %(M)sms',
            &quot;app.app_runner:app&quot;
    ]
    sys.exit(gunicorn.run())

if __name__ == &quot;__main__&quot;:
    run()
</code></pre>
<hr />
<p>PS. Whether I set worker number by <code>limits CPU (10*2+1=21)</code> or <code>requests CPU (3*2+1=7)</code> the performance still can't catch up with my expectations. Any trial suggestions to improve performance will be welcome under this questions</p>

<blockquote>
<p>how to calculate the worker number? should I use limits CPU or requests CPU?</p>
</blockquote>
<p>It depends on your situation. First, look at the documentation about <a href="https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/#requests-and-limits" rel="nofollow noreferrer">request and limits</a> (this example is for memory, but the same is for CPU).</p>
<blockquote>
<p>f the node where a Pod is running has enough of a resource available, it's possible (and allowed) for a container to use more resource than its  <code>request</code>  for that resource specifies. However, a container is not allowed to use more than its resource  <code>limit</code>.</p>
<p>For example, if you set a  <code>memory</code>  request of 256 MiB for a container, and that container is in a Pod scheduled to a Node with 8GiB of memory and no other Pods, then the container can try to use more RAM.</p>
<p>If you set a  <code>memory</code>  limit of 4GiB for that container, the kubelet (and  <a href="https://kubernetes.io/docs/setup/production-environment/container-runtimes" rel="nofollow noreferrer">container runtime</a>) enforce the limit. The runtime prevents the container from using more than the configured resource limit. For example: when a process in the container tries to consume more than the allowed amount of memory, the system kernel terminates the process that attempted the allocation, with an out of memory (OOM) error.</p>
</blockquote>
<p>Answering your question: first of all, you need to know how many resources (eg. CPU) your application needs. Request will be the minimum amount of CPU that the application must receive (you have to calculate this value yourself. In other words - you must know how much the application needs minimum CPU to run properly and then you need to set the value.) For example, if your application will perform better, when it receives more CPU, consider adding a limit ( this is the maximum amount of CPU an application can receive). If you want to calculate the worker number based on the highest performance, use <code>limit</code> to calculate the value. If, on the other hand, you want your application to run smoothly (perhaps not as fast as possible, but it will consume less resources) use <code>request</code> type.</p>
