Django + WSGI: problems with upgrade?

I am developing a Django site. I make all my changes on a real server, simply because it's easier. The problem is that from time to time it seems like it caches one of the * .py files I'm working on. Sometimes, if I swipe the update, it will switch between the old version of the page and the newer version.

My setup is more or less similar to what is described in the Django tutorials: http://docs.djangoproject.com/en/dev/howto/deployment/modwsgi/#howto-deployment-modwsgi

I assume this does this because it runs multiple instances of the WSGI handler and depending on which handler sends the http request, I may get different versions of the page. Restarting apache seems to fix the problem, but it is annoying.

I really don't know much about WSGI or MiddleWare, or about any of these query processing materials. I come from the background of PHP where it all works :)

Anyway, what is a good way to solve this problem? Will the daemon mode work with the WSGI handler to fix the problem? If so, how do I get it to work in daemon mode?

+7
django apache mod-wsgi
source share
4 answers

You can solve this problem without editing your code on a real server. Seriously, there is no excuse. Develop locally with version control, and if you need, start your server from live verification using a post-commit hook that checks your latest version and restarts Apache.

+2
source share

Starting the process in daemon mode will not help. Here's what happens:

mod_wsgi generates several identical processes to handle incoming requests for your Django site. Each of these processes is its own Python interpreter and can process an incoming web request. These processes are permanent (they are not brought up and do not break down for each request), so one process can process thousands of requests one after another. mod_wsgi is able to handle several web requests at the same time, since there are several processes.

Each Python interpreter process will load your modules (your custom Python files) whenever an "import module" is executed. In the django context, this will happen when a new view.py is needed due to a web request. Once the module is loaded, it is in memory, so any changes you make to the file will not be reflected in this process. As more web requests arrive, the Python process interpreter will simply use the version of the module that is already loaded into memory. You see discrepancies between updates, as each web request you make can be handled by different processes. Some processes may have loaded your Python modules during previous changes to your code, while others may have loaded them later (because these processes did not receive a web request).

A simple solution: every time you change your code, restart the Apache process. In most cases, it is as simple as launching root from the "restart / etc / init.d / apache2" shell. I believe that a simple reboot also works, which is faster, "/etc/init.d/apache2 reload"

Daemon solution: if you use mod_wsgi in daemon mode, all you have to do is click (unix command) or modify the wsgi script file. To clarify the scrompt.com message, changes to the Python source code will not cause mod_wsgi to reload your code. Rebooting occurs only when the wsgi script file has been modified.

One final note: I only talked about wsgi as using processes for simplicity. wsgi actually uses thread pools within each process. I did not feel this detail was relevant to this answer, but you can learn more by reading mod_wsgi .

+16
source share

Read the mod_wsgi documentation, rather than relying on the minimum information for the mod_wsgi host contained on the Django website. In partcular, read:

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

This tells us exactly how source code reloading works in mod_wsgi, including a monitor, which you can use to implement the same source code reload as the Django server. Also see which ones talk about how to apply this to Django.

http://blog.dscpl.com.au/2008/12/using-modwsgi-when-developing-django.html http://blog.dscpl.com.au/2009/02/source-code-reloading-with -modwsgi-on.html

+5
source share

Since you use mod_wsgi in native mode, your changes are not automatically displayed. You see them every once in a while, because Apache sometimes launches new instances of handlers that catch updates.

You can solve this using the daemon mode as described here . In particular, you will want to add the following directives to your Apache configuration:

WSGIDaemonProcess example.com processes=2 threads=15 display-name=%{GROUP} WSGIProcessGroup example.com 
+4
source share

All Articles