9

我正在开发一个 Django 站点。我正在实时服务器上进行所有更改,只是因为这样更容易。问题是,它似乎时不时地喜欢缓存我正在处理的 *.py 文件之一。有时,如果我经常刷新,它会在旧版本的页面和新版本之间来回切换。

我的设置或多或少类似于 Django 教程中描述的内容:http: //docs.djangoproject.com/en/dev/howto/deployment/modwsgi/#howto-deployment-modwsgi

它这样做是因为它启动了 WSGI 处理程序的多个实例,并且根据 http 请求发送到的处理程序,我可能会收到不同版本的页面。重新启动apache似乎可以解决问题,但这很烦人。

我真的不太了解 WSGI 或“MiddleWare”或任何请求处理的东西。我来自 PHP 背景,一切正常 :)

无论如何,解决这个问题的好方法是什么?运行 WSGI 处理程序是否会以“守护程序模式”缓解问题?如果是这样,我如何让它在守护程序模式下运行?

4

4 回答 4

18

Running the process in daemon mode will not help. Here's what's happening:

mod_wsgi is spawning multiple identical processes to handle incoming requests for your Django site. Each of these processes is its own Python Interpreter, and can handle an incoming web request. These processes are persistent (they are not brought up and torn down for each request), so a single process may handle thousands of requests one after the other. mod_wsgi is able to handle multiple web requests simultaneously since there are multiple processes.

Each process's Python interpreter will load your modules (your custom Python files) whenever an "import module" is executed. In the context of django, this will happen when a new view.py is needed due to a web request. Once the module is loaded, it resides in memory, and so any changes you make to the file will not be reflected in that process. As more web requests come in, the process's Python interpreter will simply use the version of the module that is already loaded in memory. You are seeing inconsistencies between refreshes since each web request you are making can be handled by different processes. Some processes may have loaded your Python modules during earlier revisions of your code, while others may have loaded them later (since those processes had not received a web request).

The simple solution: Anytime you modify your code, restart the Apache process. Most times that is as simple as running as root from the shell "/etc/init.d/apache2 restart". I believe a simple reload works as well, which is faster, "/etc/init.d/apache2 reload"

The daemon solution: If you are using mod_wsgi in daemon mode, then all you need to do is touch (unix command) or modify your wsgi script file. To clarify scrompt.com's post, modifications to your Python source code will not result in mod_wsgi reloading your code. Reloading only occurs when the wsgi script file has been modified.

Last point to note: I only spoke about wsgi as using processes for simplicity. wsgi actually uses thread pools inside each process. I did not feel this detail to be relevant to this answer, but you can find out more by reading about mod_wsgi.

于 2009-12-05T02:30:40.927 回答
5

因为您在嵌入式模式下使用 mod_wsgi,所以不会自动看到您的更改。您偶尔会看到它们,因为 Apache 有时会启动新的处理程序实例,这些实例会捕获更新。

您可以使用守护程序模式解决此问题,如此所述。具体来说,您需要将以下指令添加到您的 Apache 配置中:

WSGIDaemonProcess example.com processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup example.com
于 2009-10-28T09:33:49.090 回答
5

阅读 mod_wsgi 文档,而不是依赖 Django 站点上包含的 mod_wsgi 托管的最少信息。特别是,阅读:

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

This tells you exactly how source code reloading works in mod_wsgi, including a monitor you can use to implement same sort of source code reloading that Django runserver does. Also see which talks about how to apply that 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

于 2009-10-28T09:37:52.010 回答
3

您可以通过不在实时服务器上编辑代码来解决此问题。说真的,没有任何借口。使用版本控制进行本地开发,如果必须,从实时结帐运行您的服务器,并使用 post-commit 挂钩检查您的最新版本并重新启动 Apache。

于 2009-10-28T09:36:26.500 回答