Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Difference between using server through manage.py and other servers like gunicorn etc. Which is better?

I have been running my beginner Django projects with manage.py runserver. I see suggestions to use gunicorn instead. What is the difference ?

like image 936
Rishi Saraswat Avatar asked Feb 26 '16 16:02

Rishi Saraswat


People also ask

Is Gunicorn needed for Django?

Gunicorn is a WSGI server This way, when coding up your a Django application you don't need to find your own solutions for: communicating with multiple web servers. reacting to lots of web requests at once and distributing the load. keepiung multiple processes of the web application running.

What server does Django use for development?

Django's primary deployment platform is WSGI, the Python standard for web servers and applications. Django's startproject management command sets up a minimal default WSGI configuration for you, which you can tweak as needed for your project, and direct any WSGI-compliant application server to use.

Which is better Gunicorn or uWSGI?

Both can reach very impressive levels of performance, though some have mentioned that Gunicorn works better under high load. Drawbacks to Gunicorn are much the same as uWSGI, though I personally have found Gunicorn to be more easily configurable than uWSGI.

Does Django has its own server?

Django uses their own web server, which is not supposed to be used in a production setting. DO NOT USE THIS SERVER IN A PRODUCTION SETTING.


1 Answers

nginx and gunicorn are probably the most popular configuration for production deployments. Before detailing why gunicorn is recommended over runserver, let's quickly clarify the difference between nginx and gunicorn, because both state that they are web servers.

NGINX should be your entrance point to the public, it's the server listening to port 80 (http) and 443 (https). Its main purpose is handling HTTP requests, that is applying redirects, HTTP Auth if required, managing TSL/SSL Certificates and - among other things - decides where your requests is finally going to. E.g. there maybe a node.js app living on localhost:3000 that awaits requests on/foo/api while gunicorn is waiting at localhost:8000 to serve your awesome app. This functionality of proxying incoming requests to so called upstream services (in this case node.js and gunicorn) is called reverse-proxy.

GUNICORN is a server that translates HTTP requests into Python. WSGI is one of the interfaces/implementations that does that (e.g., the text parts of http headers are transformed into key-value dicts).

Django's built-in development web server (what you get when you run manage.py runserver) provides that functionality also, but it targets a development environment (e.g., restart when the code changes), whereas Gunicorn targets production.

Gunicorn has many features that Django's built-in server is lacking:

  • gunicorn can spawn multiple worker processes to parallelize incoming requests to multiple CPU cores
  • gunicorn has better logging
  • gunicorn is generally optimized for speed
  • gunicorn can be configured to fine grades depending on your setup
  • gunicorn is actively designed and maintained with security in mind

There are web servers other than gunicorn, but gunicorn (inspired by ruby's unicorn) is very popular and easy to setup, and hence is not only a good starting point, but a professional solution that is used by large projects.

like image 172
shredding Avatar answered Oct 02 '22 01:10

shredding