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

旧巷老猫 提交于 2019-12-03 05:25:43

问题


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?


回答1:


Create a file wsgi.py under your project with the following contents, then point Gunicorn at it.

from my_project import create_app

app = create_app()
gunicorn -w 4 my_project.wsgi:app
# -w 4 specifies four worker processes

Gunicorn allows specifying a function call like my_project:create_app(). For most cases, you can the skip making a wsgi.py file and tell Gunicorn how to create your app directly.

gunicorn -w 4 my_project:create_app()

Note that you may have to put the name in quotes for some shells.

gunicorn -w 4 "my_project:create_app()"

Due to a change in Gunicorn 20, the ability to call a factory function is temporarily unavailable. This PR adds it back when 20.0.1 is released.




回答2:


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

from 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.



来源:https://stackoverflow.com/questions/25319690/how-do-i-run-a-flask-app-in-gunicorn-if-i-used-the-application-factory-pattern

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!