简体   繁体   English

当名称从“应用程序”更改时,Gunicorn 无法找到应用程序

[英]Gunicorn can't find app when name changed from "application"

I use gunicorn --workers 3 wsgi to run my Flask app.我使用gunicorn --workers 3 wsgi来运行我的 Flask 应用程序。 If I change the variable application to myapp , Gunicorn gives the error AppImportError: Failed to find application: 'wsgi' .如果我将变量application更改为myappAppImportError: Failed to find application: 'wsgi'会给出错误AppImportError: Failed to find application: 'wsgi' Why am I getting this error and how do I fix it?为什么我会收到此错误以及如何修复它?

myproject.py : myproject.py

from flask import Flask

myapp = Flask(__name__)

@myapp.route("/")
def hello():
    return 'Test!'

if __name__ == "__main__":
    myapp.run(host='0.0.0.0')

wsgi.py : wsgi.py :

from myproject import myapp

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

Gunicorn (and most WSGI servers) defaults to looking for the callable named application in whatever module you point it at. Gunicorn(和大多数 WSGI 服务器)默认在您指向它的任何模块中查找可调用的命名application Adding an alias from myproject import myapp as application or application = myapp will let Gunicorn discover the callable again.添加别名from myproject import myapp as applicationapplication = myapp将使 Gunicorn 再次发现可调用对象。

However, the wsgi.py file or the alias aren't needed, Gunicorn can be pointed directly at the real module and callable.但是, wsgi.py文件或别名,Gunicorn 可以直接指向真正的模块并可调用。

gunicorn myproject:myapp --workers 16
# equivalent to "from myproject import myapp as application"

Gunicorn can also call an app factory, optionally with arguments, to get the application object. Gunicorn 还可以调用应用程序工厂(可选带参数)来获取应用程序对象。 (This briefly did not work in Gunicorn 20, but was added back in 20.0.1.) (这在 Gunicorn 20 中暂时不起作用,但在 20.0.1 中重新添加。)

gunicorn 'myproject.app:create_app("production")' --workers 16
# equivalent to:
# from myproject.app import create_app
# application = create_app("production")

For WSGI servers that don't support calling a factory, or for other more complicated imports, a wsgi.py file is needed to do the setup.对于不支持调用工厂的 WSGI 服务器,或者对于其他更复杂的导入,需要一个wsgi.py文件来进行设置。

from myproject.app import create_app
app = create_app("production")
gunicorn wsgi:app --workers 16

如果您尝试在server/cats.py为具有变量名称app提供server/cats.py ,您可以在端口 8000 上启动服务器,如下所示:

gunicorn server.cats:app -b 0.0.0.0:8000

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM