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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With