加快第一页加载在django [英] Speeding Up the First Page Load in django

查看:158
本文介绍了加快第一页加载在django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我更新我的网站上的代码时,我自然会重新启动我的apache实例,以使更改生效。



不幸的是,每个apache服务的第一页实例是相当慢的,它首次将所有内容加载到RAM(这个特定站点的5-7秒)。



后续请求只需要0.5 - 1.5秒,所以我想要为我的用户消除这种影响。



有没有更好的方法来将所有内容都加载到RAM中,而不是做一个wget x次(其中x是在我的http.conf中由ServerLimit定义的apache实例)



编写重新启动apache并运行wget 5次的重新启动脚本似乎对我来说很奇怪。 b
$ b

谢谢!

解决方案

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



假设守护进程模式,这是更好的选择,这意味着你会有一些像:

 #定义流程组。 

WSGIDaemonProcess django display-name =%{GROUP}

#挂载应用程序。

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

#确保应用程序预加载在进程启动时。必须指定要使用的
#进程组和应用程序组(Python解释器)。

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

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

#确保应用程序在同一进程组和应用程序
#组中运行,并预先加载到进程启动中。

WSGIProcessGroup django
WSGIApplicationGroup%{GLOBAL}

订单拒绝,允许
允许从所有
< / Directory>

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



使用WSGIDaemonProcess的display-name选项可以使用BSD风格'ps'程序。将'display-name'设置为'%{GROUP}','ps'输出应该显示'(wsgi:django)'作为进程名称。识别流程ID,然后执行以下操作:

  kill -SIGINT pid 

使用实际进程ID交换pid。如果守护进程组中有多个进程,请发送所有信号。



不知道是否可以在一步中使用'killall'。我在MacOS X上有问题。



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

 #定义流程组。 

WSGIDaemonProcess django display-name =%{GROUP}

#挂载应用程序并指定哪个进程组和
#应用程序组(Python解释器)运行它作为
#进程组和应用程序组命名,这将在进程启动时具有
#预加载应用程序的副作用。

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

< Directory / usr / local / django / mysite / apache>
订单拒绝,允许
允许从所有
< / Directory>

也就是说,不需要使用单独的WSGIImportScript指令,特定的进程组和应用程序组作为参数WSGIScriptAlias代替它会预加载应用程序的副作用。


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!

解决方案

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.

这篇关于加快第一页加载在django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆