Skip to content Skip to sidebar Skip to footer

Initialising A Flask App Running With Apache And Mod_wsgi

I've got a Flask app running under Apache using mod_wsgi. The app needs to do do some initialisation, including setting some top-level variables that need to be accessible inside t

Solution 1:

It is happening on first request because by default mod_wsgi will only load your WSGI script file when the first request arrives. That is, it lazily loads your WSGI application.

If you want to force it to load your WSGI application when the process first starts, then you need to tell mod_wsgi to do so.

If you have configuration like:

WSGIDaemonProcess myapp
WSGIProcessGroup myapp
WSGIApplicationGroup %{GLOBAL}
WSGIScriptAlias / /some/path/app.wsgi

change it to:

WSGIDaemonProcess myapp
WSGIScriptAlias / /some/path/app.wsgi process-group=myapp application-group=%{GLOBAL}

It is only when both process group and application group are specified on WSGIScriptAlias, rather than using the separate directives, that mod_wsgi can know up front what process/interpreter context the WSGI application will run in and so preload the WSGI script file.

BTW, if you weren't already using mod_wsgi daemon mode (the WSGIDaemonProcess directive), and forcing the main interpreter context (the WSGIApplicationGroup %{GLOBAL} directive), you should be, as that is the preferred setup.

Post a Comment for "Initialising A Flask App Running With Apache And Mod_wsgi"