Softsofter Softsofter -4 years ago 204
Python Question

how to config Django and how to run Gunicorn workers with cpu performance?

Im using,

gunicorn django_project.wsgi:application --bind=127.0.0.1:8866 --daemon
as command line to run my django on server with 6 processors and 14gb ram, but I did not setup workers, I am using 2 applications on this server, how can I get maximum performance, using all ram memory and processors.

Answer Source

You can pass a command line argument to specify the number of workers you wish to run, see: http://docs.gunicorn.org/en/stable/settings.html#worker-processes

However, if you wish to programmatically get the number of cores, better pass a parameter to read the configuration from a module, like:

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

And the contents of gunicorn.py.ini would be:

from multiprocessing import cpu_count

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

There is also a common formula used for specifying the number of workers:

workers = cpu_count() * 2 + 1

See the reasoning behind this on: http://docs.gunicorn.org/en/stable/design.html#how-many-workers

Regarding memory usage, I think there's not much you can do, they're gonna use as much memory as they need. However, there are a couple more things you might try out, to optimize your workers. You can specify the worker_class in the configuration file, for instance:

worker_class = 'gevent'

And test which one suits you the best, see the list of available worker classes on: http://docs.gunicorn.org/en/stable/settings.html#worker-class

Also, by specifying the max_requests option (http://docs.gunicorn.org/en/stable/settings.html#max-requests), you can force the workers to be restarted after a specified number of requests. This is essentially useful if your code leaks memory for some reason, so when they are restarted, the os will clean up after them.

max_requests = 1000  # a reasonable value
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download