Gunicorn can't find app when name changed from “application”

匿名 (未验证) 提交于 2019-12-03 02:18:01

问题:

I use gunicorn --workers 3 wsgi to run my Flask app. If I change the variable application to myapp, Gunicorn gives the error AppImportError: Failed to find application: 'wsgi'. Why am I getting this error and how do I fix it?

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:

from myproject import myapp  if __name__ == "__main__":     myapp.run() 

回答1:

Gunicorn (and most WSGI servers) defaults to looking for the callable named application or app in whatever module you point it at. Simply adding an alias from myproject import myapp as application or application = myapp will let Gunicorn discover the callable again.

However, the wsgi.py file or the alias aren't needed, Gunicorn can be pointed directly at the real module and callable. There is almost never a good reason to have a separate wsgi.py file.

gunicorn myproject:myapp --workers 16 


回答2:

If you're trying to serve an app named app within server/app.py, you can start the server on port 8000 as follows:

gunicorn server.app:app -b 0.0.0.0:8000 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!