使用Gunicorn时Flask secret_key不可用,即使它已配置

urb*_*urb 0 python flask gunicorn

我正在尝试部署一个简单的烧瓶应用程序.然后我选择了gunicorn和nginx.但是当我尝试使用gunicorn运行应用程序时,会出现以下异常:

RuntimeError: the session is unavailable because no secret key was set.  Set the secret_key on the application to something unique and secret.
Run Code Online (Sandbox Code Playgroud)

init.py:

if __name__ == '__main__':
    app.secret_key = config["secret-key"]
    app.run(port=config["port"], host=config["host"], debug=config["debug"])
Run Code Online (Sandbox Code Playgroud)

dav*_*ism 5

__name__防护的全部意义在于导入模块时代码不会执行.WSGI服务器导入您的模块并使用Flask应用程序可调用,因此代码不会被执行.将配置移到__name__防护外部.