Skip to main content
 首页 » 编程设计

python之使用 gunicorn 进行部署时,我应该为多核 CPU 创建多少个 worker

2024年11月01日10pander-it

我正在使用 gunicorn 部署我的网络服务。该文档建议在以下网页中使用示例配置文件进行部署:http://docs.gunicorn.org/en/latest/configure.html .

代码如下:

import multiprocessing 
 
bind = "127.0.0.1:8000" 
workers = multiprocessing.cpu_count() * 2 + 1 

我不知道为什么我们要使用这个奇怪的计数:“num_of_cpu_core * 2 + 1”。为什么不直接使用 CPU 核心数?

请您参考如下方法:

他们的 architecture page在选择 worker 数量方面给出了更多建议:

While not overly scientific, the formula is based on the assumption that for a given core, one worker will be reading or writing from the socket while the other worker is processing a request.

“为什么不使用内核数作为工作线程数”的答案是,有时工作线程会阻塞(例如在 IO 操作期间),因此拥有额外的工作线程可以更好地利用 CPU。