简体   繁体   English

Gunicorn 20 在 'index' 中找不到应用程序 object 'app.server'

[英]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作为入口点只需要进行两项更改:

  1. Import both app and server into index.pyappserver都导入index.py

from webapp import app, server

  1. Run gunicorn as follows运行 gunicorn 如下

gunicorn -b localhost:8000 index:server

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在“ app” gunicorn中找不到应用程序对象“ app” - Failed to find application object 'app' in 'app' gunicorn Gunicorn:在“运行”中找不到应用程序 object“App” - Gunicorn : Failed to find application object 'App' in 'run' 错误:gunicorn:无法在“app”中找到应用程序 object“app” - Error: gunicorn: Failed to find application object 'app' in 'app' FLASK:plotly:错误:模块“索引”没有属性“app.server” - FLASK : plotly : Error: module 'index' has no attribute 'app.server' 运行 docker 时无法将“app.server”解析为属性名称或 function 调用 - Failed to parse 'app.server' as an attribute name or function call when running docker Gunicorn 在 Flask 应用程序的服务器挂钩中出现“应用程序 object 必须是可调用的”失败,但前提是作为服务运行 - Gunicorn fails with "Application object must be callable" in server hook for Flask app, but only if run as service Gunicorn:尝试启动 flask 服务器时无法在 'wsgi' 中找到属性 'app' - Gunicorn: Failed to find attribute 'app' in 'wsgi' when attempting to start flask server 当名称从“应用程序”更改时,Gunicorn 无法找到应用程序 - Gunicorn can't find app when name changed from "application" Gunicorn无法加载Flask应用程序 - Gunicorn failed to load Flask application 在 Heroku 上部署 Postgres Flask 应用程序,“gunicorn.errors.AppImportError:无法在 'app' 中找到属性 'app'。” - Deploying Postgres Flask App on Heroku, "gunicorn.errors.AppImportError: Failed to find attribute 'app' in 'app'."
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM