如何配置Django以及如何使用cpu性能运行Gunicorn worker?

Sof*_*ter 3 python django performance gunicorn server

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

and*_*ean 9

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

但是,如果您希望以编程方式获取内核数量,请更好地传递参数以从模块中读取配置,例如:

gunicorn django_project.wsgi:application -c gunicorn.py.ini
Run Code Online (Sandbox Code Playgroud)

内容gunicorn.py.ini将是:

from multiprocessing import cpu_count

bind = '127.0.0.1:8866'
daemon = True
workers = cpu_count()
Run Code Online (Sandbox Code Playgroud)

还有一个用于指定工人数量的通用公式:

workers = cpu_count() * 2 + 1
Run Code Online (Sandbox Code Playgroud)

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

关于内存使用情况,我认为你可以做的并不多,他们会根据需要使用尽可能多的内存.但是,您可以尝试更多的方法来优化您的员工.您可以worker_class在配置文件中指定,例如:

worker_class = 'gevent'
Run Code Online (Sandbox Code Playgroud)

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

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

max_requests = 1000  # a reasonable value
Run Code Online (Sandbox Code Playgroud)