简体   繁体   English

加速django中的第一页加载

[英]Speeding Up the First Page Load in django

When I update the code on my website I (naturally) restart my apache instance so that the changes will take effect. 当我更新我的网站上的代码时,我(自然地)重新启动我的apache实例,以便更改生效。

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). 不幸的是,每个apache实例所服务的第一个页面非常慢,而它第一次将所有内容加载到RAM中(此特定站点为5-7秒)。

Subsequent requests only take 0.5 - 1.5 seconds so I would like to eliminate this effect for my users. 后续请求只需0.5到1.5秒,所以我想为我的用户消除这种影响。

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) 是否有更好的方法将所有内容加载到RAM而不是执行wget x次(其中x是我的http.conf中ServerLimit定义的apache实例的数量)

Writing a restart script that restarts apache and runs wget 5 times seems kind of hacky to me. 编写一个重新启动apache并运行wget 5次的重启脚本对我来说似乎有些不好意思。

Thanks! 谢谢!

The default for Apache/mod_wsgi is to only load application code on first request to a process which requires that applications. Apache / mod_wsgi的默认设置是仅在第一次请求时将应用程序代码加载到需要该应用程序的进程。 So, first step is to configure mod_wsgi to preload your code when the process starts and not only the first request. 因此,第一步是配置mod_wsgi以在进程启动时预加载代码,而不仅仅是第一个请求。 This can be done in mod_wsgi 2.X using the WSGIImportScript directive. 这可以使用WSGIImportScript指令在mod_wsgi 2.X中完成。

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. 进行代码更改后,不要触摸仅在下一个请求中检查的WSGI脚本文件,而是向守护进程组中的进程发送SIGINT信号。

With the 'display-name' option to WSGIDaemonProcess you can identify which processes by using BSD style 'ps' program. 使用WSGIDaemonProcess的'display-name'选项,您可以使用BSD样式'ps'程序识别哪些进程。 With 'display-name' set to '%{GROUP}', the 'ps' output should show '(wsgi:django)' as process name. 将'display-name'设置为'%{GROUP}'时,'ps'输出应显示'(wsgi:django)'作为进程名称。 Identify the process ID and do: 确定进程ID并执行:

kill -SIGINT pid

Swap 'pid' with actual process ID. 用实际进程ID交换'pid'。 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. 不确定'killall'是否可用于一步完成此操作。 I had problem with doing it on MacOS X. 我在MacOS X上执行此操作时遇到问题。

In mod_wsgi 3.X the configuration can be simpler and can use instead: 在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>

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. 也就是说,不需要使用单独的WSGIImportScript指令,因为特定的进程组和应用程序组可以作为WSGIScriptAlias的参数,而不是副作用,它将预加载应用程序。

How are you running Django (mod_python vs mod_wsgi)? 你是如何运行Django的(mod_python vs mod_wsgi)?

If you're running mod_wsgi (in daemon mode), restarting Apache isn't necessary to reload your application. 如果您正在运行mod_wsgi(在守护程序模式下),则无需重新启动Apache即可重新加载应用程序。 All you need to do is update the mtime of your wsgi script (which is done easily with touch ). 您需要做的就是更新wsgi脚本的mtime(通过touch可以轻松完成)。

mod_wsgi's documentation has a pretty thorough explanation of the process: mod_wsgi的文档对该过程有一个非常详尽的解释:

ReloadingSourceCode ReloadingSourceCode

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM