Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask application on uwsgi gives a TypeError: 'Flask' object is not iterable

I'm running Python/Flask application on Python 3.5 in a virtualenv on Arch Linux. The application is run by a uwsgi server that is connected via socket to Nginx.

When I perform a request, I get the following uwsgi error:

Mar 23 02:38:19 saltminion1.local uwsgi[20720]: TypeError: 'Flask' object is not iterable

This is the callable that uwsgi is configured to use:

def create_app(config=None, import_name=None):
    if import_name is None:
        import_name = DefaultConfig.PROJECT

    app = Flask(import_name, instance_path=INSTANCE_FOLDER_PATH, instance_relative_config=True)

    configure_app(app, config)
    configure_database(app)
    configure_logging(app)
    configure_error_handlers(app)
    configure_blueprints(app)

    return app

Things work fine when I start the application using the built-in HTTP server both on the local OS X development workstation and on Arch/Ubuntu vagrant boxes.

Problem is: After adding debug statements it became clear the error occurs at some point in the Flask code itself and not within my app. How can I get a stack trace here to troubleshoot better?

like image 440
Cedric Meury Avatar asked Mar 22 '16 15:03

Cedric Meury


1 Answers

A WSGI app (which Flask is), is a callable object. That's what uWSGI expects to be passed to callable. You're passing an app factory, which is also callable, but you need to pass it the result of that call, because the app factory isn't a WSGI application itself.

The factory function can be called directly in the configuration. The module and callable options can also be combined in just module.

module = my_app:create_app()

This tells uWSGI to import my_app, find my_app.create_app, and call it. The result of that, the Flask app, is what's actually used as the callable.

like image 162
davidism Avatar answered Sep 30 '22 17:09

davidism