Gunicorn 20 在 'index' 中找不到应用程序对象 'app.server'
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.
This issue on Gunicorn's issue tracker 似乎与错误有关,但我无法弄清楚我应该做什么来修复它。如何让 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
作为入口点。您将所有内容放在单独的顶级模块中,而不是放在一个包中。
如果您想将应用程序设置与索引视图分开,您应该遵循标准的 Flask 模式来定义应用程序,然后导入视图,所有这些都在一个包中。
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
结构为多页应用程序 (https://dash.plotly.com/urls) 的 Dash 项目将具有 app.py
(此处命名为 webapp.py
)和 index.py
。如链接指南中所述,入口点应为 index.py
以防止循环导入。
使用index.py
作为入口点只需要两处改动:
- 将
app
和 server
都导入 index.py
from webapp import app, server
- 运行 gunicorn如下
gunicorn -b localhost:8000 index:server
我正在使用 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.
This issue on Gunicorn's issue tracker 似乎与错误有关,但我无法弄清楚我应该做什么来修复它。如何让 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
作为入口点。您将所有内容放在单独的顶级模块中,而不是放在一个包中。
如果您想将应用程序设置与索引视图分开,您应该遵循标准的 Flask 模式来定义应用程序,然后导入视图,所有这些都在一个包中。
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
结构为多页应用程序 (https://dash.plotly.com/urls) 的 Dash 项目将具有 app.py
(此处命名为 webapp.py
)和 index.py
。如链接指南中所述,入口点应为 index.py
以防止循环导入。
使用index.py
作为入口点只需要两处改动:
- 将
app
和server
都导入index.py
from webapp import app, server
- 运行 gunicorn如下
gunicorn -b localhost:8000 index:server