加快 django 中的首页加载速度
当我更新网站上的代码时,我(自然)会重新启动我的 apache 实例,以便更改生效。
不幸的是,每个 apache 实例提供的第一个页面在第一次将所有内容加载到 RAM 中时非常慢(对于这个特定站点需要 5-7 秒)。
后续请求只需要 0.5 - 1.5 秒,因此我想为我的用户消除这种影响。
有没有比执行 wget x 次更好的方法将所有内容加载到 RAM 中(其中 x 是我的 http.conf 中 ServerLimit 定义的 apache 实例的数量)
编写一个重新启动 apache 并运行 wget 5 次的重新启动脚本似乎很不错对我来说很哈克。
谢谢!
When I update the code on my website I (naturally) restart my apache instance so that the changes will take effect.
Unfortunately the first page served by each apache instance is quite slow while it loads everything into RAM for the first time (5-7 sec for this particular site).
Subsequent requests only take 0.5 - 1.5 seconds so I would like to eliminate this effect for my users.
Is there a better way to get everything loaded into RAM than to do a wget x times (where x is the number of apache instances defined by ServerLimit in my http.conf)
Writing a restart script that restarts apache and runs wget 5 times seems kind of hacky to me.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Apache/mod_wsgi 的默认设置是仅在第一次请求需要该应用程序的进程时加载应用程序代码。因此,第一步是配置 mod_wsgi 在进程启动时预加载代码,而不仅仅是第一个请求。这可以在 mod_wsgi 2.X 中使用 WSGIImportScript 指令来完成。
假设守护进程模式(无论如何这是更好的选择),这意味着您将有类似的情况:
当您进行代码更改时,不要触摸仅在下一个请求时检查的 WSGI 脚本文件,而是向进程发送 SIGINT 信号而是在守护进程组中。
通过 WSGIDaemonProcess 的“display-name”选项,您可以使用 BSD 风格的“ps”程序来识别哪些进程。将“display-name”设置为“%{GROUP}”时,“ps”输出应显示“(wsgi:django)”作为进程名称。识别进程 ID 并执行以下操作:
将“pid”替换为实际进程 ID。如果守护进程组中有多个进程,则向所有进程发送信号。
不确定是否可以使用“killall”一步完成此操作。我在 MacOS X 上执行此操作时遇到问题。
在 mod_wsgi 3.X 中,配置可以更简单,并且可以使用:
也就是说,不需要使用单独的 WSGIImportScript 指令,因为可以将特定进程组和应用程序组作为 WSGIScriptAlias 的参数,而不是使用 side其效果是它将预加载应用程序。
The default for Apache/mod_wsgi is to only load application code on first request to a process which requires that applications. So, first step is to configure mod_wsgi to preload your code when the process starts and not only the first request. This can be done in mod_wsgi 2.X using the WSGIImportScript directive.
Presuming daemon mode, which is better option anyway, this means you would have something like:
When you have made a code change, instead of touch the WSGI script file, which is only checked on the next request, send a SIGINT signal to the processes in the daemon process group instead.
With the 'display-name' option to WSGIDaemonProcess you can identify which processes by using BSD style 'ps' program. With 'display-name' set to '%{GROUP}', the 'ps' output should show '(wsgi:django)' as process name. Identify the process ID and do:
Swap 'pid' with actual process ID. If more than one process in daemon process group, send signal to all of them.
Not sure if 'killall' can be used to do this in one step. I had problem with doing it on MacOS X.
In mod_wsgi 3.X the configuration can be simpler and can use instead:
That is, no need to use separate WSGIImportScript directive as can specific process group and application group as arguments to WSGIScriptAlias instead with side effect that it will preload application.
你如何运行 Django(mod_python 与 mod_wsgi)?
如果您正在运行 mod_wsgi(在守护程序模式下),则无需重新启动 Apache 即可重新加载您的应用程序。您所需要做的就是更新 wsgi 脚本的 mtime(使用
touch
可以轻松完成)。mod_wsgi 的文档对该过程有非常全面的解释:
ReloadingSourceCode
How are you running Django (mod_python vs mod_wsgi)?
If you're running mod_wsgi (in daemon mode), restarting Apache isn't necessary to reload your application. All you need to do is update the mtime of your wsgi script (which is done easily with
touch
).mod_wsgi's documentation has a pretty thorough explanation of the process:
ReloadingSourceCode