Initialising a Flask app running with Apache and mod_wsgi Initialising a Flask app running with Apache and mod_wsgi flask flask

Initialising a Flask app running with Apache and mod_wsgi


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 myappWSGIProcessGroup myappWSGIApplicationGroup %{GLOBAL}WSGIScriptAlias / /some/path/app.wsgi

change it to:

WSGIDaemonProcess myappWSGIScriptAlias / /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.