Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Django handle multiple requests?

How does Django handles multiple requests in production environment?

Suppose we have one of web server: Apache, Nginx, gunicorn etc.
So do those servers for any request from web browser start new process to serve that request?
If it's true, doesn't it cause huge overhead?
If it's not true, then how the same view (let it be def hello(request) view bound to /hello url) serve several requests at the same time.

I've seen answers for question "... handle multiple users"

like image 695
Alex-droid AD Avatar asked Mar 10 '18 20:03

Alex-droid AD


People also ask

How does Django handle multiple users?

Django doesn't do anything to handle this at all. It's entirely the job of whatever server is serving Django, be it Apache, gunicorn, uwsgi, or whatever. Those servers are responsible for managing the threads or processes to serve multiple requests.

How many API requests can Django handle?

Django development server which you run on local machine using command python manage.py runserver can handle only 1 request at a time.

Can Django handle millions of requests?

They can handle over one million responses every day on their rewritten Python website. Quora is the number one place online to ask a question and receive answers from a community of individuals.

How many requests can Django handle per second?

As we're approaching 8 billion page views per month and 45k requests per second, we've learned a couple things about delivering comments to a lot of different people. Disqus is very well known for using Django for almost all of our web traffic, and that continues to be a thing today.


1 Answers

Django handles just a request at a time.

If you use the very old CGI interface (between your web-server and Django), a new Django process is started at every request. But I think nobody do this.

There are many additional interfaces on web servers, not do load at every request a new server side program. FastCGI is one of these (agnostic to programming language), some programs have own module directly implemented in web server (e.g. mod-php) [python had this in the past]. But now Django and in general python, prefer WSGI interface.

So webserver open one or more programs (Django app) in parallel. The web server will send request to a free process (or it queue requests, this is handled by web server). How many processes, and for how long, it depend on web server configuration.

The databases supported by django supports concurrency, so there is no problem on having different processes handling the same app. [SQLite is different, but you should use this, just for developing/testing Django]. By writing to some log files [usually multiline], one could see some problems (parallel process which write at the same time, the same file).

NOTE: in such explanation I use "web server" in a broad sense. This includes gunicorn, mod-wsgi etc.

like image 72
Giacomo Catenazzi Avatar answered Oct 01 '22 00:10

Giacomo Catenazzi