Gunicorn won't start Flask app because "Application object must be callable"
Asked Answered
P

1

19

This is maybe a Python noob question, but after having spent a whole day searching, I couldn't find a solution.

I created a simple Flask application, here is an extract of the python_pgfutter_importer/app.py:

import os

from flask import Flask, Blueprint

from python_pgfutter_importer import settings
from python_pgfutter_importer.api.importer.business import import_csv
from python_pgfutter_importer.api.importer.endpoints.csv import ns as csv_importer_namespace
from python_pgfutter_importer.api.restplus import api

app = Flask(__name__)


def initialize_app(flask_app):
    blueprint = Blueprint('api', __name__, url_prefix='/api')
    api.init_app(blueprint)
    api.add_namespace(csv_importer_namespace)
    flask_app.register_blueprint(blueprint)


def main():
    initialize_app(app)
    app.run(debug=settings.FLASK_DEBUG, use_reloader=settings.FLASK_USE_RELOADER)


if __name__ == "__main__":
    main()

Application is working well, I start it using this command:

python python_pgfutter_importer/app.py

Now I must host this application on a server, thus I have to use WSGI. Using all the tutorials I could find, I wrote this WSGI entry point (wsgi.py):

from python_pgfutter_importer import app as application

if __name__ == "__main__":
    application.main()

When starting Gunicorn, here is the CLI output:

$gunicorn -b 127.0.0.1:8000 wsgi:application               
[2018-11-12 09:15:48 +0100] [11782] [INFO] Starting gunicorn 19.9.0
[2018-11-12 09:15:48 +0100] [11782] [INFO] Listening at: http://127.0.0.1:8000 (11782)
[2018-11-12 09:15:48 +0100] [11782] [INFO] Using worker: sync
[2018-11-12 09:15:48 +0100] [11785] [INFO] Booting worker with pid: 11785
Application object must be callable.
[2018-11-12 09:15:48 +0100] [11782] [INFO] Shutting down: Master
[2018-11-12 09:15:48 +0100] [11782] [INFO] Reason: App failed to load.

It seems the problem is: Application object must be callable.

That doesn't make any sense, since the application is Flask and is of course callable.

What can I be missing?

Thanks

Portray answered 12/11, 2018 at 8:18 Comment(0)
M
20

Given your path is python_pgfutter_importer/app.py, it looks like your import in wsgi.py is off:

from python_pgfutter_importer import app as application

This makes application a module object, what you need is an app:

from python_pgfutter_importer.app import app as application

Alternatively, you could probably do away with wsgi.py if you change wsgi:application in Gunicorn's command line to python_pgfutter_importer.app:app. Docs.

Marilyn answered 12/11, 2018 at 8:24 Comment(5)
Thanks it seems better, Gunicorn starts. But requests are not answered. I've put a log at the beginning of app.py:main() method, and it is never reached, it seems the main() is never calledPortray
@Portray It is never called because it is guarded by if __name__ == '__main__'. But then if you have Gunicorn, why do you need to run it manually?Marilyn
the main() method contains the application initialization (scheduler creation, app configuration, and finally app.run() method) Am I wrong to put this code here?Portray
@Portray You could call initialize_app() at the top level, for example see answers to this question: https://mcmap.net/q/604527/-where-in-flask-gunicorn-to-initialize-application/1983772Marilyn
Init code can indeed be put at the first level in app.py, just before main() method. Thanks @Norrius!Portray

© 2022 - 2024 — McMap. All rights reserved.