Question

This might sound stupid question so apologies in advance.

I am trying to understand how Django framework actually works behind the scenes. It's my understanding that Django does not run all the time and gets invoked by uwsgi/gunicorn or anything else when a request comes in and processed as follows:

  1. WsgiHandler or ModPythonHandler
  2. Import settings, custom exceptions
  3. Load middleware
  4. Middleware -> URLResolver
  5. Middleware -> View -> Template
  6. Middleware -> HttpResponse

But what I cannot understand that is there any part of Django which keeps running all the time like cache management or some other functions or instances rather being created per request. I would really appreciate if you can explain a bit or give pointers.

Was it helpful?

Solution

You don't say where your "understanding" comes from, but it's not really accurate. Django itself is pretty agnostic about how it runs - it depends on the server - but it's very unusual for it to be invoked from scratch on each request. About the only method where that's the case is CGI, and it'll run like a dog.

Speaking in very general terms, there are two ways Django can be run. Either it runs inside a process of the web server itself - as with mod_wsgi on Apache - or it runs in a completely separate process and receives requests via reverse proxy from the server, as with uwsgi/gunicorn. Either way, the lifetime of the Django process is not directly connected with the request, but is persistent across many requests. In the case of mod_wsgi for example, the server starts up threads and/or processes (depending on the configuration) and each one lasts for a large number of consecutive requests before being killed and restarted.

For each process, this means that any modules that have been loaded stay in memory for the lifetime of the process. Everything from the middleware onwards is executed once per request, but they wouldn't usually need to be re-imported and run each time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top