Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Django use processes or threads to handle user requests in view?

Tags:

django

Does Django use processes or threads to handle user requests in view?

If Django uses threads I can't to use all CPU cores(python global interpreter lock), if Django uses processes I can't with no worry share memory.

I tried to find the info in google, but maximum that I have managed to find is Did django use thread to handle requests? This seems isn't a answer.

like image 712
Sergey Luchko Avatar asked Nov 01 '16 11:11

Sergey Luchko


People also ask

Does Django use threads or processes?

Django's request handler is thread-safe. You can configure your WSGI server to use any number of processes (sometimes called workers) and threads per process. As you've mentioned, processes use more memory, but threads are affected by the GIL.

How does Django handle request?

Whenever a request comes into Django, it is handled by middlewares. When the Django server starts, the first thing it loads after settings.py is middlewares. The Request is processed by various middlewares one at a time. So, from the above list, when the request comes it will be passed through the security middleware.

How does Django handle multiple requests?

Can Django Handle Multiple Requests at the Same Time? Actually, Django handles only one request at a time. It processes a single request and considers the next one only after the first request is completed.

What is request in views in Django?

From Django Docs. Request came from User that want to load page. When a page is requested, Django creates an HttpRequest object that contains metadata about the request. Then Django loads the appropriate view, passing the HttpRequest as the first argument to the view function.


1 Answers

Django is run as a WSGI application. How that happens is determined by your WSGI server (e.g. uWSGI, Gunicorn, mod_wsgi).

Django's request handler is thread-safe. You can configure your WSGI server to use any number of processes (sometimes called workers) and threads per process.

As you've mentioned, processes use more memory, but threads are affected by the GIL. A good configuration should find a balance between the number of processes and the number of threads per process.

like image 88
knbk Avatar answered Oct 24 '22 15:10

knbk