TAGS :Viewed: 4 - Published at: a few seconds ago

[ How do I run a flask app in gunicorn if I used the application factory pattern? ]

I wrote a flask app using the application factory pattern. That means it doesn't create an app instance automatically when you import it. You have to call create_app for that. Now how do I run it in gunicorn?

Answer 1


Create a file wsgi.py under your project with the following contents:

from my_project import create_app

app = create_app()

Then when running gunicorn, point it to my_project.wsgi:app.

I haven't actually used gunicorn, but a common pattern in other libraries such as setuptools, uwsgi, and celery, is to allow specifying a function call like my_project:create_app(). You can try this and skip the wsgi.py file if it works.

Answer 2


You need to create_app() with specific factory config in wsgi.py just like manage.py or runserver.py. See the code below:

form your_app import create_app

app = create_app(os.getenv('FLASK_CONFIG') or 'dev')

if __name__ == "__main__":
    app.run()

And then, you could run command gunicorn -w 4 -b 0.0.0.0:5000 wsgi:app to run your application.