[英]Gunicorn 20 failed to find application object 'app.server' in 'index'
I'm using Gunicorn to deploy my Dash app.我正在使用 Gunicorn 来部署我的 Dash 应用程序。 After upgrading to Gunicorn 20.0.0, it fails to find my application.
升级到 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 seems to be related to the error, but I can't figure out what I'm supposed to do to fix it. Gunicorn 的问题跟踪器上的这个问题似乎与错误有关,但我无法弄清楚我应该做什么来修复它。 How can I make Gunicorn 20 find my app?
如何让 Gunicorn 20 找到我的应用程序?
index.py
: 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
: 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 changed how it parses and loads the application argument. Gunicorn 20 改变了它解析和加载应用程序参数的方式。 It used to use
eval
, which followed attribute access.它曾经使用
eval
,它遵循属性访问。 Now it only does a simple lookup for a single name in the given module.现在它只对给定模块中的单个名称进行简单的查找。 The ability for Gunicorn to understand Python syntax such as attribute access was not documented or intended.
Gunicorn 理解 Python 语法(例如属性访问)的能力没有记录或打算使用。
Dash's docs about deployment don't use the syntax you're using though. Dash 关于部署的文档并没有使用您正在使用的语法。 They say to do the following, which will work for any version of Gunicorn:
他们说要执行以下操作,这将适用于任何版本的 Gunicorn:
webapp.py
: webapp.py
:
server = app.server
$ gunicorn webapp:server
You're already adding the server
alias in the webapp
module, but your code layout is a bit off and making things confusing for you.您已经在
webapp
模块中添加了server
别名,但是您的代码布局有点偏离并让您感到困惑。 You're ignoring the setup you do in webapp
and using index
as your entry point instead.您忽略了在
webapp
中所做的设置,而是使用index
作为入口点。 You're putting everything in separate top-level modules rather than within a package.您将所有内容都放在单独的顶级模块中,而不是放在 package 中。
If you want to split your app setup from your index views, you should follow the standard Flask pattern of defining the app, then importing the views after that, all within a package.如果你想从索引视图中分离你的应用程序设置,你应该遵循标准的 Flask 模式来定义应用程序,然后导入视图,所有这些都在 package 中。
project/
myapp/
__init__.py
webapp.py
index.py
webapp.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
you should also import server from webapp.py to index.py and then use regular gunicorn procedure:您还应该将服务器从 webapp.py 导入到 index.py,然后使用常规 gunicorn 程序:
gunicorn index:server -b :8000
A Dash project that is structured as a multi-page app ( https://dash.plotly.com/urls ) will have app.py
(named as webapp.py
here) and index.py
.一个构建为多页应用程序的 Dash 项目( https://dash.plotly.com/urls )将具有
app.py
(此处命名为webapp.py
)和index.py
。 As mentioned in the linked guide, the entry point should be index.py
to prevent circular import.如链接指南中所述,入口点应为
index.py
以防止循环导入。
Only two changes are needed to use index.py
as the entry point:使用
index.py
作为入口点只需要进行两项更改:
app
and server
into 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.