【问题标题】:Apache Django Mod_Wsgi - auto reloadApache Django Mod_Wsgi - 自动重新加载
【发布时间】:2010-11-08 18:54:01
【问题描述】:

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

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

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


    【解决方案1】:

    阅读:

    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 相关的变化。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 您应该有某种脚本文件,您的 WSGI 应用程序将其用作入口点。那是代码所属的地方。如果您没有该文件,则需要在使用自动重新加载之前查找如何执行此操作。
      【解决方案3】:

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

      touch site.wsgi
      

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

      【讨论】:

      • 这仅适用于 UNIX 系统上的守护程序模式,不适用于 Windows。
      • 您可以直接将触摸功能添加到您的 Django 视图中。如果您使用的是 Linux,请确保与您一起运行 WSGI/Django 的用户具有使用您的入口脚本对文件的写入权限。就我而言,一切都以 Apache 用户身份运行,但 root 拥有脚本文件的所有权。
      【解决方案4】:

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

      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)
      

      【讨论】:

      • 我知道,但该代码在文件系统中的哪个位置?文件名等...
      【解决方案5】:

      我使用安装在 D:\BitNami DjangoStackC:\ 上的 Bitnami DjangoStack http://bitnami.org/stack/djangostackWindows XP 进行测试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 方法替换为 @ 987654324@,这里是脚本部分

      ....
      
      _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 和监控代码。它们是两种不同的技术。第一个将在每个请求时重新启动。后者仅在进行代码更改时才会重新启动。如果您设置指令,监控代码将变得毫无意义,因为它将重新启动每个请求。
      【解决方案6】:

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

      WSGIScriptReloading On
      

      然后重新加载 Apache

      systemctl reload apache2
      

      享受吧!

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

      【讨论】:

        猜你喜欢
        • 2011-05-11
        • 2013-06-08
        • 1970-01-01
        • 2012-03-17
        • 2012-03-17
        • 1970-01-01
        • 2014-10-06
        • 2011-02-04
        • 2011-01-22
        相关资源
        最近更新 更多