I am trying to build a flask app and using mod_wsgi server for hosting. I use a factory function to create the flask app but I observe that mod-wsgi calls the factory function twice within the same process on initialization.
To demonstrate this, I added some print statements in the factory function and they get printed twice in apache logs when I start the wsgi server. I want to start some background threads in the factory function but if mode_wsgi calls the factory twice, it will start multiple background thread which I don't want.
Can someone tell me why mod_wsgi calls the flask factory function twice on app initialization and how do I stop it? I am running flask in production mode.
apache logs:
root@728d78f92b2a:/tmp/mod_wsgi-localhost:9090:0# cat error_log
[Fri Sep 29 20:23:45.172948 2023] [mpm_event:notice] [pid 6:tid 281472976408608] AH00489: Apache/2.4.57 (Debian) mod_wsgi/4.9.4 Python/3.8 configured -- resuming normal operations
[Fri Sep 29 20:23:45.172965 2023] [wsgi:error] [pid 8:tid 281472976408608] mod_wsgi (pid=8): Unsupported locale setting en_US.UTF-8 specified for daemon process group localhost:9090. Consider using 'C.UTF-8' as fallback setting.
[Fri Sep 29 20:23:45.173002 2023] [core:notice] [pid 6:tid 281472976408608] AH00094: Command line: 'apache2 (mod_wsgi-express) -f /tmp/mod_wsgi-localhost:9090:0/httpd.conf -D MOD_WSGI_KEEP_ALIVE -D MOD_WSGI_MPM_ENABLE_EVENT_MODULE -D MOD_WSGI_MPM_EXISTS_EVENT_MODULE -D MOD_WSGI_MPM_EXISTS_WORKER_MODULE -D MOD_WSGI_MPM_EXISTS_PREFORK_MODULE -D FOREGROUND'
[Fri Sep 29 20:23:45.289648 2023] [wsgi:error] [pid 8:tid 281472976408608] inside factory function!
[Fri Sep 29 20:23:45.289670 2023] [wsgi:error] [pid 8:tid 281472976408608] flask-env production
[Fri Sep 29 20:23:45.290326 2023] [wsgi:error] [pid 8:tid 281472976408608] inside factory function!
[Fri Sep 29 20:23:45.290338 2023] [wsgi:error] [pid 8:tid 281472976408608] flask-env production
init.py
from flask import Flask
from src.app import create_app
application = create_app()
app.py
from flask import Flask
import os
def create_app():
app = Flask('sample-app')
print("inside factory function!")
print(f"flask-env {os.environ['FLASK_ENV']}")
# start bg threads here
# app initialization
@app.route('/', methods=['GET'])
def hello_world():
return "Hello! this is a sample flask app!"
return app
running mod-wsgi from a dockerfile:
...
...
CMD ["mod_wsgi-express", "start-server", "/sample-flask-app/src/__init__.py", "--user", "www-data", "--group", "www-data", "--port", "9090"]
I was expecting mod_wsgi to call the factory function only once per process but thats not whats happening here. I am not providing any custom apache configuration
My best guess is that it is called twice, as your factory function is within the directory's
__init__.pyfile.It could be that some subsequent import within your project structure imports something from the
srcdirectory again, which leads to another execution of the__init__.pymodule, which then calls create_app() another time.Tough to tell without knowing your full setup, but as first try I would propose to move the code, which shall be run by mod_wsgi, into another python module than the directories
__init__.py