views:

40

answers:

1

My mod_wsgi django application seems to keep getting reloaded for the first several requests that the client makes. This is killing my performance

After enough requests it seems to settle down, and the application no longer seems to be getting reloaded. Any thoughts on why this is happening and how I can prevent it?

(I have the following in httpd.conf:MaxRequestsPerChild 0, so that isn't it)

+3  A: 

This is likely because you are using embedded mode of mod_wsgi and Apache on a UNIX system, possibly even with Apache prefork MPM which makes it all worse. In short, in that configuration Apache it is a multi process web server. Combine that with fact that default is to lazily load application on first request, you will see a delay on initial request against each Apache server child process as application loads.

Even for Django framework this shouldn't be excessive and would question what your specific application is doing on startup to cause a long delay or large load spike.

To understand the issues, make sure you read:

http://blog.dscpl.com.au/2009/03/load-spikes-and-excessive-memory-usage.html http://code.google.com/p/modwsgi/wiki/ProcessesAndThreading

Then change to use daemon mode of mod_wsgi instead as documented on mod_wsgi wiki pages. In particular start with:

http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide

If it is truly warranted that you need to run more than one daemon process and not just being hopeful about what sort of load your application is going to get, and load time is still a concern, then you can configure mod_wsgi using WSGIImportScript and other methods to preload your WSGI application at process start before any requests come in. For Django though, make sure you use WSGI script file described in:

http://blog.dscpl.com.au/2010/03/improved-wsgi-script-for-use-with.html

and not the one that Django documentation describes as it lazily loads and you can still see problem as well as differences in behaviour between WSGI hosting mechanisms and built in development server.

Graham Dumpleton