[英]Gunicorn 20 failed to find application object 'app.server' in 'index'
我正在使用 Gunicorn 来部署我的 Dash 应用程序。 升级到 Gunicorn 20.0.0 后,找不到我的应用程序。
gunicorn --bind=0.0.0.0 --timeout 600 index:app.server
Failed to find application object 'app.server' in 'index'
[INFO] Shutting down: Master
[INFO] Reason: App failed to load.
Gunicorn 的问题跟踪器上的这个问题似乎与错误有关,但我无法弄清楚我应该做什么来修复它。 如何让 Gunicorn 20 找到我的应用程序?
index.py
:
import os
from dash.dependencies import Input, Output
import dash_core_components as dcc
import dash_html_components as html
from pages import overview
from webapp import app
app.index_string = open(os.path.join("html", "index.html")).read()
app.layout = html.Div([
dcc.Location(id="url", refresh=False),
html.Div(id="page-content")
])
@app.callback(Output("page-content", "children"), [Input("url", "pathname")])
def display_page(pathname):
if pathname == "/a-service/overview":
return overview.layout
else:
return overview.layout
if __name__ == "__main__":
app.run_server(debug=True, port=8051)
webapp.py
:
import dash
description = "a description"
title = "a title"
creator = "@altf1be"
app = dash.Dash(
__name__,
meta_tags=[
{"name": "viewport", "content": "width=device-width, initial-scale=1"},
{"name": "description", "content": description},
{"name": "twitter:description", "content": description},
{"property": "og:title", "content": description},
{"name": "twitter:creator", "content": creator}
]
)
server = app.server
app.config.suppress_callback_exceptions = True
Gunicorn 20 改变了它解析和加载应用程序参数的方式。 它曾经使用eval
,它遵循属性访问。 现在它只对给定模块中的单个名称进行简单的查找。 Gunicorn 理解 Python 语法(例如属性访问)的能力没有记录或打算使用。
Dash 关于部署的文档并没有使用您正在使用的语法。 他们说要执行以下操作,这将适用于任何版本的 Gunicorn:
webapp.py
:
server = app.server
$ gunicorn webapp:server
您已经在webapp
模块中添加了server
别名,但是您的代码布局有点偏离并让您感到困惑。 您忽略了在webapp
中所做的设置,而是使用index
作为入口点。 您将所有内容都放在单独的顶级模块中,而不是放在 package 中。
如果你想从索引视图中分离你的应用程序设置,你应该遵循标准的 Flask 模式来定义应用程序,然后导入视图,所有这些都在 package 中。
project/
myapp/
__init__.py
webapp.py
index.py
webapp.py
:
app = dash.Dash(...)
server = app.server
# index imports app, so import it after app is defined to avoid a circular import
from myapp import index
$ gunicorn myapp.webapp:server
您还应该将服务器从 webapp.py 导入到 index.py,然后使用常规 gunicorn 程序:
gunicorn index:server -b :8000
一个构建为多页应用程序的 Dash 项目( https://dash.plotly.com/urls )将具有app.py
(此处命名为webapp.py
)和index.py
。 如链接指南中所述,入口点应为index.py
以防止循环导入。
使用index.py
作为入口点只需要进行两项更改:
app
和server
都导入index.py
from webapp import app, server
gunicorn -b localhost:8000 index:server
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.