python - mod_wsgi : Reload Code via Inotify - not every N seconds

标签 python django mod-wsgi inotify

到目前为止,我按照这个建议重新加载代码:

https://code.google.com/archive/p/modwsgi/wikis/ReloadingSourceCode.wiki

这有一个缺点,即代码更改仅每 N 秒检测一次。我可以使用 N=0.1,但这会导致无用的磁盘 IO。

据我所知,linux 内核的 inotify 回调可通过 python 获得。

有没有更快的方法来检测代码更改并重新启动 wsgi 处理程序?

我们在 linux 上使用守护进程模式。

为什么要为 mod_wsgi 重新加载代码

有人对我为什么想要这个很感兴趣。这是我的设置:

大多数人使用“manage.py runserver”进行开发和其他一些wsgi为生产部署。

在我看来,我们已经自动创建了新系统,生产和开发系统大多相同。

一个操作系统(linux)可以承载N个系统(虚拟环境)。

开发人员可以使用 runserver 或 mod_wsgi。使用 runserver 的好处是易于调试,mod_wsgi 的好处是您无需先启动服务器。

mod_wsgi 的好处是您知道 URL:https://dev-server/system-name/myurl/

使用 runserver 你不知道端口。用例:您想从内部 wiki 链接到开发系统....

为 mod_wsgi 重新加载代码的肮脏 hack,我们过去使用过:maximum-requests=1 但这很慢。

最佳答案

预赛。

Developers can use runserver or mod_wsgi. Using runserver has the benefit that you it easy for debugging, mod_wsgi has the benefit that you don't need to start the server first.

但是您需要先设置服务器,这需要付出很多努力。服务器也需要在这里启动,尽管您可以将其配置为在启动时自动启动。

如果您在端口 80 或 443 上运行(通常是这种情况),则服务器只能由 root 启动。如果需要重新启动,您将不得不再次请求 super 用户的帮助。所以 ./manage.py runserver 在这里得分很高。

mod_wsgi has the benefit, that you know the URL: https://dev-server/system-name/myurl/

这与开发服务器没有什么不同。默认情况下,它在端口 8000 上启动,因此您可以通过 http://dev-server:8000/system-name/myurl/ 访问它。 .如果你想在开发服务器上使用 SSL,你可以使用像 django-sslserver 这样的包。或者你可以将 nginx 放在 django 开发服务器的前面。

With runserver you don't know the port. Use case: You want to link from >an internal wiki to a dev-system ....

对于 runserver,端口定义明确,如上所述。你可以让它在不同的端口上监听,例如:

 ./manage.py runserver 0.0.0.0:9090

请注意,如果您将开发服务器置于 apache(作为反向代理)或 NGINX 之后,我上面提到的重启问题等不适用于此处。

简而言之,对于开发工作,您使用 mod_wsgi 所做的一切都可以使用 django 开发服务器(又名 ./manage.py runserver)完成。

通知

终于进入正题了。假设您已经安装了 inotify-tools,您可以将它输入到您的 shell 中。您不需要编写脚本。

  while inotifywait -r -e modify .; do sudo kill -2 yourpid ; done

这将导致在...时重新加载代码

... using daemon mode with a single process you can send a SIGINT signal to the daemon process using the ‘kill’ command, or have the application send the signal to itself when a specific URL is triggered. ref: http://modwsgi.readthedocs.io/en/develop/user-guides/frequently-asked-questions.html#application-reloading

或者

while inotifywait -r -e modify .; do touch wsgi.py ; done

... using daemon mode, with any number of processes, and the process reload mechanism of mod_wsgi 2.0 has been enabled, then all you need to do is touch the WSGI script file, thereby updating its modification time, and the daemon processes will automatically shutdown and restart the next time they receive a request.

在这两种情况下,我们都使用 -r 标志来告诉 inotify 监视子目录。这意味着每次您保存 .css.js 文件时,apache 都会重新加载。但是如果没有 -r 标志,子文件夹中 python 代码的更改将不会被检测到。为了两全其美,请使用 --exclude 指令删除 css、js、图像等。

当您的 IDE 保存自动备份文件时怎么办?或者 vim 保存 .swp 文件?这也将导致代码重新加载。因此,您也必须排除这些文件类型。

所以简而言之,要免费重现 django 开发服务器所做的工作是一项艰巨的工作。

关于python - mod_wsgi : Reload Code via Inotify - not every N seconds,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37883127/

相关文章:

python 制作我自己的小写转换器

python - 获取具有 NaN 值的所有行

python - 在 Apache/mod_wsgi 上运行 Django 时出错

python - 在 Ubuntu 16.04 上找不到 django==3.0 的匹配发行版

python - 如何使用 apache 网络服务器管理与 mod_wsgi 的 session ?

带有 --enable-shared : will not build any extensions 的 Python 3.1.1

python - 如何打印出给定列表中的特定对象?

python - 虹膜上随机均匀分布

Django+gunicorn+nginx上传大文件502错误

Django 的 HttpResponseRedirect 似乎剥离了我的子域?