How to reload a configuration file on each request for Flask?
<p>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.</p>

<p>Edit: <code>app.run(debug=True)</code> is not acceptable as it restarts the server and shouldn't be used in production.</p>

<p>Perhaps a decorator like the following:</p>

<pre><code>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')
</code></pre>

<p>If it is relevant, here is how I am loading the configuration now:</p>

<p>My <code>app/app/__init__.py</code> file:</p>

<pre><code>from flask import Flask
from settings import Config

app = Flask(__name__)
app.config.from_object(Config)

# ...
</code></pre>

<p>My <code>app/app/settings.py</code> file:</p>

<pre><code>class Config(object):
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    SECRET_KEY = os.urandom(32)
    # ...

try:
   from app.local_settings import Config
except ImportError:
    pass
</code></pre>

<p>You cannot safely / correctly reload the config after the application begins handling requests.  Config is <em>only</em> meant to be read during application setup.  The main reason is because a production server will be running using multiple processes (or even distributed across servers), and the worker that handles the config change request does not have access to other workers to tell them to change.  Additionally, some config is not designed to be reloaded, so even if you could notify all the other workers and get them to reload properly, it might not have any effect.</p>

<p>Production WSGI servers can gracefully reload, that is they won't kill running workers until they've completed their responses, so downtime shouldn't actually be an issue.  If it is (and it really isn't), then you're at such a large scale that you're beyond the scope of this answer.</p>

<p>Graceful reload in:</p>

<ul>
<li><a href="http://docs.gunicorn.org/en/stable/signals.html#master-process" rel="nofollow">Gunicorn</a></li>
<li><a href="http://uwsgi-docs.readthedocs.io/en/latest/articles/TheArtOfGracefulReloading.html#standard-default-boring-graceful-reload-aka-sighup" rel="nofollow">uWSGI</a></li>
<li><a href="http://stackoverflow.com/a/3682085/400617">mod_wsgi</a></li>
</ul>

<p>If you need to use config that can be updated dynamically, you'll have to write all the code you use to expect that.  You could use a <code>before_request</code> handler to load the config fresh each request.  However, keep in mind that anything you didn't write that uses config may not expect the config to change.</p>

python flask