Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Process Lifetime

When using Django, how long does the Python process used to service requests stay alive? Obviously, a given Python process services an entire request, but is it guaranteed to survive across across requests?

The reason I ask is that I perform some expensive computations at when I import certain modules and would like to know how often the modules will be imported.

like image 876
Alex Rothberg Avatar asked Apr 08 '26 07:04

Alex Rothberg


1 Answers

This is not a function of Django at all, but of whatever system is being used to serve Django. Usually that'll be wsgi via something like mod_wsgi or a standalone server like gunicorn, but it might be something completely different like FastCGI or even plain CGI.

The point is that all these different systems have their own models that determines process lifetime. In anything other than basic CGI, any individual process will certainly serve several requests before being recycled, but there is absolutely no general guarantee of how many - the process might last several days or weeks, or just a few minutes.

One thing to note though is that you will almost always have several processes running concurrently, and you absolutely cannot count on any particular request being served by the same one as the previous one. That means if you have any user-specific data you want to persist between requests, you need to store it somewhere like the session.

like image 166
Daniel Roseman Avatar answered Apr 10 '26 20:04

Daniel Roseman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!