Django的启动code。与WSGI延迟加载 [英] Django startup code with wsgi lazy loading

查看:285
本文介绍了Django的启动code。与WSGI延迟加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Apache + mod_wsgi的Django应用程序。当启动Apache时,似乎总是WSGI其lazyloads的应用程序,这意味着启动Apache的时候没有code被执行。
只有当第一请求进入是整个应用程序加载,initalized等,并返回给用户。

I am running a Django application using Apache+mod_wsgi. When starting up Apache, it seems that the wsgi always lazyloads its app which means that no code is executed when Apache is started. Only when the first request comes in is the whole app loaded, initalized, etc and returned to the user.

因为我需要,而不必依赖于第一次请求运行一些code,我在想,如果这个懒加载能以某种方式被关闭或workedaround?

As I need to run some code without having to rely on the first request, I was wondering if this "lazy loading" can somehow be turned off or workedaround?

我试过WSGIImportScript VirtualHost指令和WSGILazyInitialization关服务器之一,但没有运气,应用程序还是懒洋洋地加载在第一次请求。

I tried the WSGIImportScript VirtualHost directive and the "WSGILazyInitialization Off" server one but without luck, the app still loads lazily on first request.

任何想法?

编辑:
要使用精确的配置更新:

To update with the exact config:


  • 在Ubuntu 13.10上运行

    • 的Apache2 2.4.6-2ubuntu2.1

    • 中的libapache2-MOD-WSGI,3.4-4

    我的在/etc/apache2/sites-enabled/000-default.conf以下内容:

    I have the following in /etc/apache2/sites-enabled/000-default.conf:

    WSGIDaemonProcess lh.test.com流程= 2个线程= 15的显示名称=%{}集团
    WSGIProcessGroup lh.test.com
    WSGIScriptAlias​​ / /home/user/myapp/src/wsgi.py
    WSGIImportScript /home/user/myapp/src/startup.py进程组=%{}全球应用程序组=%{} GLOBAL

    WSGIDaemonProcess lh.test.com processes=2 threads=15 display-name=%{GROUP} WSGIProcessGroup lh.test.com WSGIScriptAlias / /home/user/myapp/src/wsgi.py WSGIImportScript /home/user/myapp/src/startup.py process-group='%{GLOBAL}' application-group='%{GLOBAL}'

    和startup.py包括:

    and startup.py contains:

    进口WSGI
    打印ZZZZZ

    import wsgi print 'zzzzz'

    没有出现在日志中...

    Nothing appears in the logs...

    推荐答案

    在革兰氏回答这个问题<一href=\"http://stackoverflow.com/questions/16746269/when-does-apache2-execute-a-wsgi-script-when-using-daemon-process-groups/16747089#16747089\">when-does-apache2-execute-a-wsgi-script他指出,新的mod_wsgi> 3的处理了这一点。这是(简而言之)在进程组和应用程序组添加到您的WSGIScriptAlias​​

    In Gram's Answer to this question when-does-apache2-execute-a-wsgi-script he points out the new mod_wsgi > 3 handling of this. Which is (in a nutshell) add the 'process-group' and 'application-group' to your WSGIScriptAlias

    看到的功能在 ChangesInVersion0300

    ---从文档狙击---

    --- sniped from documentation ---

    有关WSGIScriptAlias​​(但不是WSGIScriptAlias​​Match),可以只在要求处理的时间进行评估,其中两个过程的基团和提供应用组的参数,并且既不使用扩展变量,这也将导致WSGI脚本文件为preloaded当过程开始时,而不是被延迟加载只有当应用第一请求到达

    For WSGIScriptAlias (but not WSGIScriptAliasMatch) where both 'process-group' and 'application-group' parameters are provided, and neither use expansion variables that can only be evaluated at the time of request handling, this will also cause the WSGI script file to be preloaded when the process starts, rather than being lazily loaded only when first request for application arrives.

    ---从最终文档狙击---

    --- end sniped from documentation ---

    试试这个:(它的工作对我来说)

    试着让你的Apache配置指令如下:

    Try making your apache configuration directives look like this:

    WSGIScriptAlias / /home/user/myapp/src/wsgi.py \
                      application-group='%{GLOBAL}' process-group='lh.test.com' 
    
    WSGIDaemonProcess lh.test.com processes=2 threads=15 display-name=%{GROUP}
    WSGIProcessGroup lh.test.com 
    # Probably wont need this anymore.
    #WSGIImportScript /home/user/myapp/src/startup.py \
    #                  process-group='%{GLOBAL}' application-group='%{GLOBAL}'
    

    现在把你的打印ZZZZZ在你/home/user/myapp/src/wsgi.py文件的底部。

    Now put your print "zzzzz" at the bottom of your /home/user/myapp/src/wsgi.py file.

    最后:如果这不会为你工作,请修改您的问题,所以你的配置是在code块,使其更具可读性。还包括在这些是在Apache配置指令。即如果他们在虚拟主机节等。由于存在与这些指令出现在哪里和什么样的顺序一些规则,人们将无法看到这个问题,如果你不包括它。

    Finally: If this does not work for you, please modify your question so your configuration is in code-blocks to make it more readable. Also include where these are in the apache configuration directives. i.e. if they are in VirtualHost sections etc. As there are some rules related to where and what order these directives appear, and people wont be able to see the problem if you dont include it.

    干杯

    这篇关于Django的启动code。与WSGI延迟加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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