加快 django 中的首页加载速度

发布于 2024-08-10 08:38:42 字数 332 浏览 9 评论 0原文

当我更新网站上的代码时,我(自然)会重新启动我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你没皮卡萌 2024-08-17 08:38:42

Apache/mod_wsgi 的默认设置是仅在第一次请求需要该应用程序的进程时加载应用程序代码。因此,第一步是配置 mod_wsgi 在进程启动时预加载代码,而不仅仅是第一个请求。这可以在 mod_wsgi 2.X 中使用 WSGIImportScript 指令来完成。

假设守护进程模式(无论如何这是更好的选择),这意味着您将有类似的情况:

# Define process group.

WSGIDaemonProcess django display-name=%{GROUP}

# Mount application.

WSGIScriptAlias / /usr/local/django/mysite/apache/django.wsgi

# Ensure application preloaded on process start. Must specify the
# process group and application group (Python interpreter) to use.

WSGIImportScript /usr/local/django/mysite/apache/django.wsgi \
  process-group=django application-group=%{GLOBAL}

<Directory /usr/local/django/mysite/apache>

    # Ensure application runs in same process group and application
    # group as was preloaded into on process start.

    WSGIProcessGroup django
    WSGIApplicationGroup %{GLOBAL}

    Order deny,allow
    Allow from all
</Directory>

当您进行代码更改时,不要触摸仅在下一个请求时检查的 WSGI 脚本文件,而是向进程发送 SIGINT 信号而是在守护进程组中。

通过 WSGIDaemonProcess 的“display-name”选项,您可以使用 BSD 风格的“ps”程序来识别哪些进程。将“display-name”设置为“%{GROUP}”时,“ps”输出应显示“(wsgi:django)”作为进程名称。识别进程 ID 并执行以下操作:

kill -SIGINT pid

将“pid”替换为实际进程 ID。如果守护进程组中有多个进程,则向所有进程发送信号。

不确定是否可以使用“killall”一步完成此操作。我在 MacOS X 上执行此操作时遇到问题。

在 mod_wsgi 3.X 中,配置可以更简单,并且可以使用:

# Define process group.

WSGIDaemonProcess django display-name=%{GROUP}

# Mount application and designate which process group and
# application group (Python interpreter) to run it in. As
# process group and application group named, this will have
# side effect of preloading application on process start.

WSGIScriptAlias / /usr/local/django/mysite/apache/django.wsgi \
  process-group=django application-group=%{GLOBAL}

<Directory /usr/local/django/mysite/apache>
    Order deny,allow
    Allow from all
</Directory>

也就是说,不需要使用单独的 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:

# Define process group.

WSGIDaemonProcess django display-name=%{GROUP}

# Mount application.

WSGIScriptAlias / /usr/local/django/mysite/apache/django.wsgi

# Ensure application preloaded on process start. Must specify the
# process group and application group (Python interpreter) to use.

WSGIImportScript /usr/local/django/mysite/apache/django.wsgi \
  process-group=django application-group=%{GLOBAL}

<Directory /usr/local/django/mysite/apache>

    # Ensure application runs in same process group and application
    # group as was preloaded into on process start.

    WSGIProcessGroup django
    WSGIApplicationGroup %{GLOBAL}

    Order deny,allow
    Allow from all
</Directory>

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:

kill -SIGINT pid

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:

# Define process group.

WSGIDaemonProcess django display-name=%{GROUP}

# Mount application and designate which process group and
# application group (Python interpreter) to run it in. As
# process group and application group named, this will have
# side effect of preloading application on process start.

WSGIScriptAlias / /usr/local/django/mysite/apache/django.wsgi \
  process-group=django application-group=%{GLOBAL}

<Directory /usr/local/django/mysite/apache>
    Order deny,allow
    Allow from all
</Directory>

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.

套路撩心 2024-08-17 08:38:42

你如何运行 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文