Apache Django Mod_Wsgi - 自动重新加载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1093732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 17:51:22  来源:igfitidea点击:

Apache Django Mod_Wsgi - auto reload

djangoapachemod-wsgi

提问by Eeyore

I am trying to auto reload my django app which uses apache + mod_wsgi on my local windows machine.

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

I'd like to know where do I add this code that's referenced in the following article:

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

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

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)

回答by Graham Dumpleton

Read:

读:

http://blog.dscpl.com.au/2008/12/using-modwsgi-when-developing-django.html

http://blog.dscpl.com.au/2008/12/using-modwsgi-when-developing-django.html

It tells you exactly where to place the file when using Django. You just need to make the code change that everyone is pointing out to you in the source code reloading documentation section related to Windows. Also read:

它会告诉您在使用 Django 时确切放置文件的位置。您只需要更改每个人都在与 Windows 相关的源代码重新加载文档部分中向您指出的代码更改。另请阅读:

http://blog.dscpl.com.au/2009/02/source-code-reloading-with-modwsgi-on.html

http://blog.dscpl.com.au/2009/02/source-code-reloading-with-modwsgi-on.html

which explains the variations on the first related to Windows.

这解释了第一个与 Windows 相关的变化。

回答by Xeningem

I use this code on my server

我在我的服务器上使用此代码

touch site.wsgi

and it work. After reload page in browser I get page with changes. May be it ugly - but simple and no necessary restart apache.

它工作。在浏览器中重新加载页面后,我得到了带有更改的页面。可能很丑 - 但很简单,不需要重新启动 apache。

回答by AlbertoPL

You replace the restart function that is mentioned in the block of code above in the same article.

您替换了同一篇文章中上面代码块中提到的重启函数。

回答by wlashell

You replace the restart function in the following block of code you find on the page:

您在页面上找到的以下代码块中替换了重新启动功能:

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)

回答by tsurahman

I test this with Bitnami DjangoStackhttp://bitnami.org/stack/djangostackand Windows XPinstalled on D:\BitNami DjangoStackand C:\Documents and Settings\tsurahman\BitNami DjangoStack projects\myprojectas project directory (default install)

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

as in http://code.google.com/p/modwsgi/wiki/ReloadingSourceCode#Restarting_Apache_Processes, I added

就像在http://code.google.com/p/modwsgi/wiki/ReloadingSourceCode#Restarting_Apache_Processes 中一样,我添加了

MaxRequestsPerChild 1
MaxRequestsPerChild 1

in file D:\BitNami DjangoStack\apps\django\conf\django.confsee comment by Graham Dumpleton

在文件D:\BitNami DjangoStack\apps\django\conf\django.conf 中看到 Graham Dumpleton 的评论

then I created a file monitor.pyin my project directorywith content as in http://code.google.com/p/modwsgi/wiki/ReloadingSourceCode#Monitoring_For_Code_Changesand replace the _restartmethod with http://code.google.com/p/modwsgi/wiki/ReloadingSourceCode#Restarting_Windows_Apache, here is the part of the script

然后我创建了一个文件monitor.py在我的项目目录与内容在http://code.google.com/p/modwsgi/wiki/ReloadingSourceCode#Monitoring_For_Code_Changes和更换_restart与方法http://code.google.com /p/modwsgi/wiki/ReloadingSourceCode#Restarting_Windows_Apache,这里是脚本的一部分

....

_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:

....

and in file D:\BitNami DjangoStack\apps\django\scripts\django.wsgi,

并在文件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()

and then restart the Apache server

然后重启Apache服务器