[英]mod_wsgi on Python 3.10 does not update Django templates without Apache restart
I have been running a Django server on Python 3.8 with Apache and mod_wsgi for some time now, and decided it was time to upgrade to Python 3.10.我已经使用 Apache 和 mod_wsgi 在 Python 3.8 上运行 Django 服务器一段时间了,我决定是时候升级到 Python 3.10 了。 I installed Python 3.10 on my server, installed the Django and mod_wsgi packages, copied the 3.10 build of mod_wsgi.so to Apache's modules folder, and everything works great... however, it seems with this build of mod_wsgi, changes to template files do not take effect unless I restart Apache.
我在我的服务器上安装了 Python 3.10,安装了 Django 和 mod_wsgi 包,将 mod_wsgi.so 的 3.10 构建复制到 Apache 的模块文件夹,一切都很好......但是,似乎对于这个 mod_wsgi 构建,对模板文件的更改确实除非我重新启动 Apache,否则不会生效。
As an example, I added a random HTML template file to my website, and started the server with some initial text in that template.例如,我向我的网站添加了一个随机的 HTML 模板文件,并使用该模板中的一些初始文本启动了服务器。 Running on the Python 3.8, I am able to change the contents of that template (eg
echo "More Text" >> test_template.html
) and upon refreshing my browser the new text will show up.在 Python 3.8 上运行,我能够更改该模板的内容(例如
echo "More Text" >> test_template.html
),刷新浏览器后新文本将显示。 However doing the same test in 3.10, the new text will not show up.但是在 3.10 中做同样的测试,新文本不会出现。 I have tried different browser sessions and hard reloading, it is not a caching issue on the client side, and looking at the response sizes in Apache's access log confirms the data being sent to the client changes in 3.8 but not in 3.10.
我尝试了不同的浏览器会话和硬重新加载,这不是客户端的缓存问题,查看 Apache 访问日志中的响应大小可以确认发送到客户端的数据在 3.8 中发生了变化,但在 3.10 中没有发生变化。
I have stood up a test server to isolate the problem, and have narrowed it down to specifically changing the mod_wsgi build (which of course changes the entire Python version used by Django).我建立了一个测试服务器来隔离问题,并将其缩小到专门更改 mod_wsgi 构建(这当然会更改 Django 使用的整个 Python 版本)。 Still, that confirms it should not be a caching setting of Apache, or any mis-configuration of Django templates, and I have followed the steps here to confirm I am running mod_wsgi in Daemon mode (as I have been for years on this server, this is a long-standing server configured seemingly without issue for Python 3.8).
仍然,这确认它不应该是 Apache 的缓存设置,或者 Django 模板的任何错误配置,我已经按照此处的步骤确认我正在守护进程模式下运行 mod_wsgi(因为我多年来一直在这个服务器上,这是一个长期存在的服务器,配置似乎没有问题 Python 3.8)。
Lastly, running the Django development server (using base manage.py runserver
command) reflects template changes on the fly without issue, and without a server reboot.最后,运行 Django 开发服务器(使用 base
manage.py runserver
命令)反映了动态的模板更改,没有问题,并且没有重新启动服务器。 So as far as I can tell this seems to be a mod_wsgi quirk.据我所知,这似乎是一个 mod_wsgi 怪癖。
The specific Apache |具体Apache | mod_wsgi |
mod_wsgi | Python version combinations is as follows:
Python版本组合如下:
Apache/2.4.41 (Ubuntu) mod_wsgi/4.6.8 Python/3.8 Apache/2.4.41 (Ubuntu) mod_wsgi/4.6.8 Python/3.8
Apache/2.4.41 (Ubuntu) mod_wsgi/4.9.4 Python/3.10 Apache/2.4.41 (Ubuntu) mod_wsgi/4.9.4 Python/3.10
...as reported by Apache's error.log, confirming the modules are loading as expected. ...如 Apache 的 error.log 所报告,确认模块正在按预期加载。
Does anyone know if this is a known issue with Python 3.10 builds of mod_wsgi?有谁知道这是否是 Python 3.10 版 mod_wsgi 的已知问题? Perhaps is there a new setting I'm forgetting?
也许我忘记了一个新设置? My understanding of Django templates is that they should always reflect changes immediately (without a server restart), however code changes require a restart (or touching of the wsgi.py script);
我对 Django 模板的理解是它们应该始终立即反映更改(无需重新启动服务器),但是代码更改需要重新启动(或接触 wsgi.py 脚本); I have never had to restart the server for template changes prior to this change.
在此更改之前,我从未需要为模板更改重新启动服务器。 Any help is appreciated-
任何帮助表示赞赏-
Edit: Just tried upgrading my Python 3.8 version of mod_wsgi to the same version (4.9.4), and it still works fine, so there is something about Python 3.10 vs 3.8, or another installed python package. I will keep testing...编辑:刚刚尝试将我的 Python 3.8 版本的 mod_wsgi 升级到相同版本(4.9.4),它仍然可以正常工作,所以有一些关于 Python 3.10 vs 3.8,或另一个安装的 python package。我会继续测试......
I ended up making a post under the mod_wsgi github project, and traced the issue back to a change in Django's behavior in this commit我最终在 mod_wsgi github 项目下发表了一篇文章,并将问题追溯到这次提交中 Django 行为的变化
Full details of that post can be found here可以在此处找到该帖子的完整详细信息
Tl;dr; TL;博士; is there is a caching template loader, which used to only be enabled when
DEBUG = False
was set, but was updated to always be in effect.是否有一个缓存模板加载器,过去仅在设置
DEBUG = False
时启用,但已更新为始终有效。 I'm not sure why my Python 3.8 build did not have this change, as I had upgraded both builds to the latest Django build available (4.6.1), but my original install was years ago so it's quite possible a fresh install would not have had this issue.我不确定为什么我的 Python 3.8 版本没有这个变化,因为我已经将这两个版本升级到最新的 Django 可用版本 (4.6.1),但我的原始安装是几年前的,所以很可能全新安装不会有这个问题。
If you still want to disable cached templates as I did, you have to override the template loaders in your Django settings:如果你仍然想像我一样禁用缓存模板,你必须在你的 Django 设置中覆盖模板加载器:
TEMPLATES = [
{
'OPTIONS': {
'loaders': ['django.template.loaders.filesystem.Loader', 'django.template.loaders.app_directories.Loader']
},
},
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.