tags:

views:

143

answers:

2

Can a Django app be reloaded on every request ?

This is very useful for development. Ruby on Rails does just this.

  1. runserver reloads, but it reloads slow, and still sometime one has to stop and start it again for some changes to show up. (For example changes in admin.)
  2. mod_wsgi can autoreload on Linux by touching *.wsgi files. On Windows one has to use an observer/reloader script, that again is slow.
  3. I have no tried mod_python or fastcgi, can they do this?

The reason behind this is when changing scripts one would like changes to show up immediately.

+2  A: 

Of course it is going to be slow to reload, it has to load all the application code again and not just the one file. Django is not PHP, so don't expect it to work the same.

If you really want Django to reload on every request regardless, then use CGI and a CGI/WSGI bridge. It is still going to be slow though as CGI itself adds additional overhead.

The Apache/mod_wsgi method of using a code monitor, which works for daemon mode when using UNIX, or if using Windows, is the best compromise. That is, it checks once a second for any code file which is a part of the application being changed and only then restarting the process. The run server itself also uses this one second polling method from memory as well.

Using this polling approach does introduce a one second window where you may make a request before code reload requirement has been detected. Most people aren't that quick though to progress from saving a file to reloading in browser and so wouldn't notice.

In Apache/mod_wsgi 3.0 there are mechanisms which would allow one to implement an alternative code reloader that eliminates that window by being able to schedule a check for modified code at the start of a request, but this is then going to impact the performance of every request. For the polling method it runs in the background and so doesn't normally cause any performance impact on requests.

Even in Apache/mod_wsgi with current versions you could do the same by using embedded mode and setting Apache MaxRequestsPerChild to 1, but this is also going to affect performance of serving static files as well.

In short, trying to force a reload on every request is just not the best way of going about it and certainly will not eliminate the load delays resulting from using a fat Python web application such as Django.

Graham Dumpleton
`./manage.py runfcgi` supports a maxrequests argument as well, which can be set to 1 to force reloads. http://docs.djangoproject.com/en/dev/howto/deployment/fastcgi/
Adam Backstrom
A: 

Python is not PHP thank god for that!! , but ruby isn't php either, and still it has lightning fast reload. So django is weak in this regard it must improve! Also MaxRequestPerChild set to 1 or 1..20 basically makes development unfeasible, i have tried both settings, the page just won't load. Imagine a page with 50 images... That would cause 5o reloads of the complete apache process. On windows that's very slow as well. And sad but true, i am a lot faster than django reloader, i have a shortcut for the browser in the 2nd window to reload whenever i save my django code. Works perfect with Rails. (through webbrick and the other one mongrel or something)

I'll check manage.py runfcgi now.

Attila