Apache Django Mod_Wsgi - 自动重载

5

我正在尝试自动重新加载我的Django应用程序,该应用程序在本地Windows机器上使用Apache + mod_wsgi。

我想知道在以下文章中引用的代码应添加在哪里:

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

def _restart(path):
    _queue.put(True)
    prefix = 'monitor (pid=%d):' % os.getpid()
    print >> sys.stderr, '%s Change detected to \'%s\'.' % (prefix, path)
    print >> sys.stderr, '%s Triggering Apache restart.' % prefix
    import ctypes
    ctypes.windll.libhttpd.ap_signal_parent(1)
6个回答

5

1

我在我的服务器上使用这段代码

touch site.wsgi

它可以工作。在浏览器中重新加载页面后,我得到了带有更改的页面。 可能不太美观 - 但简单且无需重新启动Apache。


1
这仅适用于UNIX系统上的守护进程模式,而不适用于Windows。 - Graham Dumpleton
您可以直接将触摸功能添加到Django视图中。如果您使用的是Linux,请确保您运行WSGI / Django的用户具有对包含入口脚本的文件的写访问权限。在我的情况下,所有内容都是以Apache用户身份运行的,但root拥有脚本文件的所有权。 - Jesuisme

1

您可以在同一篇文章中的代码块中替换提到的重新启动函数。


1
你应该拥有某种脚本文件作为WSGI应用程序的入口点。那里是代码所属的地方。如果你没有那个文件,你需要在使用自动重新加载之前先了解如何创建它。 - AlbertoPL

1

0

您可以在页面上找到的以下代码块中替换重启函数:

Monitoring For Code Changes

The use of signals to restart a daemon process could also be employed in a mechanism which automatically detects changes to any Python modules or dependent files. This could be achieved by creating a thread at startup which periodically looks to see if file timestamps have changed and trigger a restart if they have.

Example code for such an automatic restart mechanism which is compatible with how mod_wsgi works is shown below.

import os
import sys
import time
import signal
import threading
import atexit
import Queue

_interval = 1.0
_times = {}
_files = []

_running = False
_queue = Queue.Queue()
_lock = threading.Lock()

def _restart(path):
    _queue.put(True)
    prefix = 'monitor (pid=%d):' % os.getpid()
    print >> sys.stderr, '%s Change detected to \'%s\'.' % (prefix, path)
    print >> sys.stderr, '%s Triggering process restart.' % prefix
    os.kill(os.getpid(), signal.SIGINT)

我知道,但是那段代码在文件系统中的哪里?文件名是什么,等等... - Eeyore

0

我使用Bitnami DjangoStackhttp://bitnami.org/stack/djangostack和安装在Windows XP上的D:\BitNami DjangoStack,以及C:\Documents and Settings\tsurahman\BitNami DjangoStack projects\myproject作为项目目录(默认安装)进行测试。

如同http://code.google.com/p/modwsgi/wiki/ReloadingSourceCode#Restarting_Apache_Processes所述,我添加了

MaxRequestsPerChild 1

在文件D:\BitNami DjangoStack\apps\django\conf\django.conf中看Graham Dumpleton的评论
然后我在我的项目目录下创建了一个文件monitor.py,内容如http://code.google.com/p/modwsgi/wiki/ReloadingSourceCode#Monitoring_For_Code_Changes所述,并用http://code.google.com/p/modwsgi/wiki/ReloadingSourceCode#Restarting_Windows_Apache替换了_restart方法,这是脚本的一部分。
....

_running = False
_queue = Queue.Queue()
_lock = threading.Lock()

def _restart(path):
    _queue.put(True)
    prefix = 'monitor (pid=%d):' % os.getpid()
    print >> sys.stderr, '%s Change detected to \'%s\'.' % (prefix, path)
    print >> sys.stderr, '%s Triggering Apache restart.' % prefix
    import ctypes
    ctypes.windll.libhttpd.ap_signal_parent(1)

def _modified(path):
    try:

....

并且在文件D:\BitNami DjangoStack\apps\django\scripts\django.wsgi中,

....

import django.core.handlers.wsgi

import monitor
monitor.start(interval=1.0)
monitor.track(os.path.join(os.path.dirname(__file__), 'site.cf'))

application = django.core.handlers.wsgi.WSGIHandler()

然后重新启动Apache服务器


你不应该同时使用MaxRequestPerChild设置为1和监视器代码。它们是两种不同的技术。第一种将在每个请求时重新启动。后者只会在进行代码更改时重新启动。如果您设置了指令,监视器代码就变得无意义了,因为它无论如何都会在每个请求时重新启动。 - Graham Dumpleton

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接