python - How to reload a configuration file on each request for Flask? -


is there idiomatic way have flask reload configuration file on every request? purpose of change passwords or other configuration related items without having shut down , restart server in production.

edit: app.run(debug=true) not acceptable restarts server , shouldn't used in production.

perhaps decorator following:

def reload_configuration(func):     @wraps(func)     def _reload_configuration(*args, **kwargs):         #even better, reload if 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 relevant, here how loading configuration now:

my app/app/__init__.py file:

from flask import flask 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:    app.local_settings import config except importerror:     pass 

you cannot safely / correctly reload config after application begins handling requests. config only meant read during application setup. main reason because production server running using multiple processes (or distributed across servers), , worker handles config change request not have access other workers tell them change. additionally, config not designed reloaded, if notify other workers , them reload properly, might not have effect.

production wsgi servers can gracefully reload, won't kill running workers until they've completed responses, downtime shouldn't issue. if (and isn't), you're @ such large scale you're beyond scope of answer.

graceful reload in:

if need use config can updated dynamically, you'll have write code use expect that. use before_request handler load config fresh each request. however, keep in mind didn't write uses config may not expect config change.


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -