Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gunicorn Flask Caching

I have a Flask application that is running using gunicorn and nginx. But if I change the value in the db, the application fails to update in the browser under some conditions.

I have a flask script that has the following commands

from msldata import app, db, models
path = os.path.dirname(os.path.abspath(__file__))
manager = Manager(app)

@manager.command
def run_dev():
    app.debug = True
    if os.environ.get('PROFILE'):
        from werkzeug.contrib.profiler import ProfilerMiddleware
        app.config['PROFILE'] = True
        app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions=[30])

    if 'LISTEN_PORT' in app.config:
        port = app.config['LISTEN_PORT']
    else:
        port = 5000

    print app.config
    app.run('0.0.0.0', port=port)
    print app.config

@manager.command
def run_server():
    from gunicorn.app.base import Application
    from gunicorn.six import iteritems

    # workers = multiprocessing.cpu_count() * 2 + 1
    workers = 1

    options = {
        'bind': '0.0.0.0:5000',
    }

    class GunicornRunner(Application):
        def __init__(self, app, options=None):
            self.options = options or {}
            self.application = app
            super(GunicornRunner, self).__init__()

        def load_config(self):
            config = dict([(key, value) for key, value in iteritems(self.options) if key in self.cfg.settings and value is not None])
            for key, value in iteritems(config):
                self.cfg.set(key.lower(), value)

        def load(self):
            return self.application

    GunicornRunner(app, options).run()
  1. Now if i run the server run_dev in debug mode db modifications are updated
  2. if run_server is used the modifications are not seen unless the app is restarted
  3. However if i run like gunicorn -c a.py app:app, the db updates are visible.

a.py contents

import multiprocessing

bind = "0.0.0.0:5000"
workers = multiprocessing.cpu_count() * 2 + 1

Any suggestions on where I am missing something..

like image 215
Lonewolf Avatar asked Aug 21 '15 21:08

Lonewolf


People also ask

Does Flask support caching?

Flask itself does not provide caching for you, but Flask-Caching, an extension for Flask does. Flask-Caching supports various backends, and it is even possible to develop your own caching backend.

Does Flask need Gunicorn?

Self-hosting Flask application with Gunicorn. Although Flask has a built-in web server, as we all know, it's not suitable for production and needs to be put behind a real web server able to communicate with Flask through a WSGI protocol. A common choice for that is Gunicorn—a Python WSGI HTTP server.


2 Answers

I also ran into this situation. Running flask in Gunicorn with several workers and the flask-cache won´t work anymore.

Since you are already using

app.config.from_object('default_config')  (or similar filename)

just add this to you config:

CACHE_TYPE = "filesystem"
CACHE_THRESHOLD = 1000000   (some number your harddrive can manage)
CACHE_DIR = "/full/path/to/dedicated/cache/directory/"

I bet you used "simplecache" before...

like image 84
mobin Avatar answered Oct 10 '22 18:10

mobin


I was/am seeing the same thing, Only when running gunicorn with flask. One workaround is to set Gunicorn max-requests to 1. However thats not a real solution if you have any kind of load due to the resource overhead of restarting the workers after each request. I got around this by having nginx serve the static content and then changing my flask app to render the template and write to static, then return a redirect to the static file.

like image 36
TCB919 Avatar answered Oct 10 '22 18:10

TCB919