有没有办法让 mod_wsgi 在每次加载时重新加载所有模块(可能在特定目录中)?
在处理代码时,每次更改时重新启动 apache 非常烦人。到目前为止我发现的唯一选择是放在modname = reload(modname)
每个导入下面.. 但这也很烦人,因为这意味着我将不得不在以后将它们全部删除..
链接:
http://code.google.com/p/modwsgi/wiki/ReloadingSourceCode
应该强调。还应该强调的是,在 UNIX 系统上必须使用 mod_wsgi 的守护程序模式,并且您必须实现文档中描述的代码监视器。整个进程重新加载选项不适用于 UNIX 系统上 mod_wsgi 的嵌入式模式。尽管在 Windows 系统上唯一的选择是嵌入式模式,但通过从代码监控脚本触发 Apache 的内部重新启动,可以通过一些技巧来做同样的事情。这也在文档中进行了描述。
以下解决方案仅针对 Linux 用户,并且已经过测试可在 Ubuntu Server 12.04.1 下运行
要在守护程序模式下运行 WSGI,您需要在 Apache 配置文件中指定WSGIProcessGroup
和WSGIDaemonProcess
指令,例如
WSGIProcessGroup my_wsgi_process
WSGIDaemonProcess my_wsgi_process threads=15
http://code.google.com/p/modwsgi/wiki/ConfigurationDirectives中提供了更多详细信息
如果您在同一台服务器下运行多个 WSGI 站点,则额外的好处是额外的稳定性,可能使用 VirtualHost 指令。在不使用守护进程的情况下,我发现两个 Django 站点相互冲突,并且交替出现 500 内部服务器错误。
此时,您的服务器实际上已经在监视您的 WSGI 站点的更改,尽管它只监视您使用 指定的文件WSGIScriptAlias
,例如
WSGIScriptAlias / /var/www/my_django_site/my_django_site/wsgi.py
这意味着您可以通过更改 WSGI 脚本来强制 WSGI 守护进程重新加载。当然,您不必更改其内容,而是,
$ touch /var/www/my_django_site/my_django_site/wsgi.py
会成功的。
通过使用上述方法,您可以在生产环境中自动重新加载 WSGI 站点,而无需重新启动/重新加载整个 Apache 服务器,或修改您的 WSGI 脚本来进行生产不安全的代码更改监控。
当您有自动部署脚本并且不想在部署时重新启动 Apache 服务器时,这特别有用。
在开发过程中,您可以使用文件系统更改观察程序touch wsgi.py
在您站点下的模块每次更改时调用,例如,pywatch
关于代码重新加载的 mod_wsgi文档是您最好的答案。
我知道这是一个旧线程,但这可能会对某人有所帮助。要在写入某个目录中的任何文件时终止您的进程,您可以使用以下内容:
import os, sys, time, signal, threading, atexit
import inotify.adapters
def _monitor(path):
i = inotify.adapters.InotifyTree(path)
print "monitoring", path
while 1:
for event in i.event_gen():
if event is not None:
(header, type_names, watch_path, filename) = event
if 'IN_CLOSE_WRITE' in type_names:
prefix = 'monitor (pid=%d):' % os.getpid()
print "%s %s/%s changed," % (prefix, path, filename), 'restarting!'
os.kill(os.getpid(), signal.SIGKILL)
def start(path):
t = threading.Thread(target = _monitor, args = (path,))
t.setDaemon(True)
t.start()
print 'Started change monitor. (pid=%d)' % os.getpid()
在您的服务器启动中,将其称为:
import monitor
monitor.start(<directory which contains your wsgi files>)
如果您的主服务器文件位于包含所有文件的目录中,您可以像这样:
monitor.start(os.path.dirname(__file__))
添加其他文件夹留作练习......
你需要'pip install inotify'
这是从这里的代码抄录而来的:https ://code.google.com/archive/p/modwsgi/wikis/ReloadingSourceCode.wiki#Restarting_Daemon_Processes
这是我在这里重复的问题的答案:WSGI process reload modules