python - 如何配置 Django 以及如何以 cpu 性能运行 Gunicorn worker?

标签 python django performance server gunicorn

我使用 gunicorn django_project.wsgi:application --bind=127.0.0.1:8866 --daemon 作为命令行在具有 6 个处理器和 14gb 内存的服务器上运行我的 django,但我做到了不是设置工作人员,我在此服务器上使用 2 个应用程序,如何使用所有 ram 内存和处理器获得最佳性能。

最佳答案

您可以传递命令行参数来指定您希望运行的工作程序数量,请参阅:http://docs.gunicorn.org/en/stable/settings.html#worker-processes

但是,如果您希望以编程方式获取核心数,最好传递一个参数以从模块读取配置,例如:

gunicorn django_project.wsgi:application -c gunicorn.py.ini

gunicorn.py.ini 的内容为:

from multiprocessing import cpu_count

bind = '127.0.0.1:8866'
daemon = True
workers = cpu_count()

还有一个常用的公式用于指定 worker 的数量:

workers = cpu_count() * 2 + 1

查看其背后的原因:http://docs.gunicorn.org/en/stable/design.html#how-many-workers

关于内存使用,我认为你无能为力,他们会根据需要使用尽可能多的内存。但是,您还可以尝试其他一些方法来优化您的工作人员。您可以在配置文件中指定worker_class,例如:

worker_class = 'gevent'

并测试哪一个最适合您,请参阅可用 worker 类别列表:http://docs.gunicorn.org/en/stable/settings.html#worker-class

此外,通过指定 max_requests 选项 ( http://docs.gunicorn.org/en/stable/settings.html#max-requests ),您可以强制 worker 在指定数量的请求后重新启动。如果您的代码由于某种原因泄漏内存,这非常有用,因此当它们重新启动时,操作系统将在它们之后进行清理。

max_requests = 1000  # a reasonable value

关于python - 如何配置 Django 以及如何以 cpu 性能运行 Gunicorn worker?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28598356/

相关文章:

python - 如何在 python 中通过套接字发送字节?

django - Userena 服务条款注册

python - 给一个对象一个参数

c# - 使用 C# 驱动程序的 MongoDB 性能较慢,而不是在命令行中

java - 使用 Java 在 MongoDB 中进行查询优化

python - 忽略 ensurepip 失败 pip 在 Ubuntu 18.04 中需要 ssl/tls 错误

Python:从列表的字符串元素创建变量/数据框名称

python - Django REST Framework 序列化一对一模型

python - 使用 MySQL Connector 和 Python 3 在 Django 中运行 inspectdb 时出错

performance - Postgres 性能 : Static row length?