我曾经gunicorn --workers 3 wsgi运行过Flask应用。如果将变量更改application为myapp,则Gunicorn给出错误AppImportError: Failed to find application: 'wsgi'。为什么会出现此错误,我该如何解决?
gunicorn --workers 3 wsgi
application
myapp
AppImportError: Failed to find application: 'wsgi'
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()
Gunicorn(和大多数WSGI服务器)默认application在你指向的模块中查找可调用的名称。添加别名from myproject import myapp as application或application = myapp将使Gunicorn再次发现可调用对象。
from myproject import myapp as application
application = myapp
Gunicorn
但是,wsgi.py不需要文件或别名,Gunicorn可以直接指向实际模块并可以调用。
gunicorn myproject:myapp --workers 16 # equivalent to "from myproject import myapp as application"
Gunicorn还可以调用应用程序工厂(可选地带参数)来获取应用程序对象。(这在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")
对于不支持调用工厂的WSGI服务器,或者对于其他更复杂的导入,wsgi.py则需要一个文件来进行设置。
from myproject.app import create_app app = create_app("production")
gunicorn wsgi:app --workers 16