使用 Gunicorn 时 Flask secret_key 不可用,即使它已配置
Flask secret_key is unavailable when using Gunicorn, even though it's configured
我正在尝试部署一个简单的 Flask 应用程序。然后我选择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.
init.py
:
if __name__ == '__main__':
app.secret_key = config["secret-key"]
app.run(port=config["port"], host=config["host"], debug=config["debug"])
__name__
守卫的全部意义在于导入模块时不会执行代码。 WSGI 服务器导入您的模块并使用可调用的 Flask 应用程序,因此代码不会被执行。将配置移到 __name__
守卫之外。
我正在尝试部署一个简单的 Flask 应用程序。然后我选择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.
init.py
:
if __name__ == '__main__':
app.secret_key = config["secret-key"]
app.run(port=config["port"], host=config["host"], debug=config["debug"])
__name__
守卫的全部意义在于导入模块时不会执行代码。 WSGI 服务器导入您的模块并使用可调用的 Flask 应用程序,因此代码不会被执行。将配置移到 __name__
守卫之外。