문제

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.

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?

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

Any idea?

EDIT: To update with the exact config:

  • running on Ubuntu 13.10
    • apache2 2.4.6-2ubuntu2.1
    • libapache2-mod-wsgi, 3.4-4
  • startup.py
  • I put 'WSGILazyInitialization Off' in /etc/apache2/mods-enabled/wsgi.conf and /etc/apache2/apache2.conf
  • I have the following in /etc/apache2/sites-enabled/000-default.conf:

    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}'

  • and startup.py contains:

    import wsgi print 'zzzzz'

Nothing appears in the logs...

도움이 되었습니까?

해결책

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

See the Features Added section in ChangesInVersion0300

--- sniped from documentation ---

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 ---

Try this: ( it worked for me )

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}'

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

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.

Cheers

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top