Apache Django Mod_Wsgi-自动重新加载

问题描述 投票:5回答:6

我正在尝试自动重新加载我的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)
django apache mod-wsgi
6个回答
5
投票

阅读:

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

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

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

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


1
投票

您替换了同一文章上面的代码块中提到的重新启动功能。


1
投票

我在服务器上使用此代码

touch site.wsgi

并且有效。在浏览器中重新加载页面后,我得到包含更改的页面。可能很丑-但很简单,无需重新启动apache。


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)

0
投票

我使用安装在D:\ BitNami DjangoStack]上的Bitnami DjangoStack http://bitnami.org/stack/djangostackWindows XPC:\ 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]中>参见格雷厄姆·邓普尔顿的评论

然后我在项目目录中创建了一个文件[[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: ....
和在文件

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服务器    

在您的虚拟主机配置文件中添加此:

WSGIScriptReloading On

并重新加载Apache

systemctl reload apache2

享受!

参考https://flask.palletsprojects.com/en/1.1.x/deploying/mod_wsgi/


0
投票
在您的虚拟主机配置文件中添加此:
© www.soinside.com 2019 - 2024. All rights reserved.