Is there an idiomatic way to have Flask reload my configuration file on every request? The purpose of this would be so that I could change passwords or other configuration related items without having to shut down and restart the server in production.
Edit: app.run(debug=True)
is not acceptable as it restarts the server and shouldn't be used in production.
Perhaps a decorator like the following:
def reload_configuration(func):
@wraps(func)
def _reload_configuration(*args, **kwargs):
#even better, only reload if the file has changed
reload(settings)
app.config.from_object(settings.Config)
return func(*args, **kwargs)
return _reload_configuration
@app.route('/')
@reload_configuration
def home():
return render_template('home.html')
If it is relevant, here is how I am loading the configuration now:
My app/app/__init__.py
file:
from flask import Flask
from settings import Config
app = Flask(__name__)
app.config.from_object(Config)
# ...
My app/app/settings.py
file:
class Config(object):
SQLALCHEMY_TRACK_MODIFICATIONS = False
SECRET_KEY = os.urandom(32)
# ...
try:
from app.local_settings import Config
except ImportError:
pass
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…