gpt4 book ai didi

python - Gunicorn 加载 Flask 应用失败

转载 作者:太空狗 更新时间:2023-10-29 17:02:07 25 4
gpt4 key购买 nike

我有一个 Flask 应用程序,我正尝试通过 Gunicorn 提供服务。

我正在使用 virtualenv 和 python3。如果我将我的 venv cd 激活到我的应用程序基本目录,然后运行:

gunicorn mysite:app

我得到:

Starting gunicorn
Listening at http://127.0.0.1:8000
DEBUG:mysite.settings:>>Config()
...
Failed to find application: 'mysite'
Worker exiting
Shutting down: master
Reason: App failed to load

查看/etc/nginx/sites-available 我只有“默认”文件。在启用站点的情况下,我没有文件。

在我的 nginx.conf 文件中我有:

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;

应用结构:

mysite    #this is where I cd to and run gunicorn mysite:app
--manage.py
--/mysite
----settings.py
----__init__.py

在我的站点的 manage.py 中,我有以下内容:

logger.debug("manage.py entry point")
app = create_app(app_name)
manager = Manager(app)

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

__init__.py文件中:

def create_app(object_name):
app = Flask(__name__)
#more setup here
return app

在我的app目录下的settings.py

class Config(object):
logger.debug(">>Config()") #this logs OK so gunicorn is at least starting in correct directory

如果我运行,则从 virtualenv 内部

print(sys.path)

我找到了这个 virtualenv 的 python 和站点包的路径。

根据我阅读的内容,启动 gunicorn 只需安装它并运行 gunicorn mysite:app

从 mysite 的父目录运行 gunicorn 我得到同样的 failed to find application: 'mysite', App failed to load 错误,但是没有记录 DEBUG...Config() (因为我们清楚地在错误的目录开始)。从 mysite/mysite 运行 gunicorn(明显错误)我在工作进程 ereor 中得到异常,导入错误:没有名为“mysite”的模块。

关于如何让 gunicorn 运行的任何线索?

最佳答案

您将 gunicorn 指向 mysite:app,这相当于 from mysite import app。但是,mysite 的顶级 (__init__.py) 级导入中没有 app 对象。让 gunicorn 给工厂打电话。

gunicorn "mysite:create_app()"

您也可以将参数传递给调用。

gunicorn "mysite:create_app('production')"

在内部,这等同于:

from mysite import create_app
app = create_app('production')

或者,您可以使用一个单独的文件来进行设置。在您的例子中,您已经在 manage.py 中初始化了一个 app

gunicorn manage:app

关于python - Gunicorn 加载 Flask 应用失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36346550/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com