问题描述
我使用应用程序工厂模式编写了flask应用程序.这意味着导入时它不会自动创建应用程序实例.您必须为此调用create_app.现在,我该如何在Gunicorn中运行它?
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?
推荐答案
在您的项目下使用以下内容创建文件wsgi.py
,然后将Gunicorn指向该文件.
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允许指定类似my_project:create_app()
的函数调用.在大多数情况下,您可以跳过制作wsgi.py
文件的步骤,并告诉Gunicorn如何直接创建您的应用.
If you're using the application factory pattern, 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 the quotes are necessary in some shells where parentheses have special meaning.
这篇关于如果使用应用程序工厂模式,如何在gunicorn中运行flask应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!