Gunicorn Flask 缓存

9

我有一个使用gunicorn和nginx运行的Flask应用程序。但是,如果我更改数据库中的值,则在某些情况下,应用程序无法在浏览器中更新。

我有一个Flask脚本,其中包含以下命令:

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. 现在,如果我以调试模式运行服务器run_dev,数据库修改将被更新。
  2. 如果使用run_server,除非重新启动应用程序,否则不会看到修改。
  3. 但是,如果我像这样运行gunicorn -c a.py app:app,则可以看到数据库更新。

a.py内容:

import multiprocessing

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

任何建议,我在哪里可能遗漏了什么...

db modifications 是如何更新的?它是否显示在网页上? - ljk321
Sequelpro 应用程序 - Lonewolf
更改为 config['CACHE_TYPE'] = 'FileSystemCache' 并设置目录。像这样 下面 - imbr
3个回答

3

我也遇到了这种情况。在使用多个工作进程运行Gunicorn中,Flask缓存将不再起作用。

既然您已经在使用

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

只需将此添加到您的配置文件中:

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

我打赌你以前用过“simplecache”...


1
我看到了相同的问题,只有在使用Flask运行Gunicorn时才会出现。一个解决方法是将Gunicorn的最大请求数设置为1。然而,如果有任何负载因为每个请求后重新启动工作进程的资源开销,那么这不是一个真正的解决方案。我通过让nginx提供静态内容,然后更改我的Flask应用程序来呈现模板并写入静态文件,然后返回到静态文件的重定向来解决这个问题。

0

Flask-Caching SimpleCache 在使用多个工作进程Gunicorn时无法正常工作

我在使用 Flask 2.02 和 Flask-Caching 1.10.1 时遇到了类似的问题。

在开发模式下一切正常,但是当你使用多个 gunicorn 工作进程时,会出现问题。其中一个可能的原因是在开发过程中只有一个进程/工作进程,所以在这种限制条件下,SimpleCache 可以正常工作。

我的代码如下:

app.config['CACHE_TYPE'] = 'SimpleCache' # a simple Python dictionary    
cache = Cache(app)

使用FileSystemCacheFlask-Caching协作的解决方案,我的代码如下:

app.config['CACHE_TYPE'] = 'FileSystemCache' 
app.config['CACHE_DIR'] = 'cache' # path to your server cache folder
app.config['CACHE_THRESHOLD'] = 100000 # number of 'files' before start auto-delete
cache = Cache(app)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接