【问题标题】:Gunicorn Flask CachingGunicorn Flask 缓存
【发布时间】:2015-11-15 22:54:21
【问题描述】:

我有一个使用 gunicorn 和 nginx 运行的 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 是如何更新的?是否显示在网页上?
  • Sequelpro 应用程序
  • 更改为config['CACHE_TYPE'] = 'FileSystemCache' 并设置目录。喜欢bellow

标签: python nginx flask gunicorn mod-wsgi


【解决方案1】:

我曾经/正在看到同样的事情,只有在使用烧瓶运行 gunicorn 时。一种解决方法是将 Gunicorn max-requests 设置为 1。但是,如果由于在每次请求后重新启动工作人员的资源开销而有任何类型的负载,那这不是一个真正的解决方案。我通过让 nginx 提供静态内容,然后更改我的烧瓶应用程序以呈现模板并写入静态内容,然后返回到静态文件的重定向来解决这个问题。

【讨论】:

    【解决方案2】:

    我也遇到过这种情况。在 Gunicorn 中与几个工人一起运行烧瓶,烧瓶缓存将不再工作。

    因为你已经在使用

    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”...

    【讨论】:

      【解决方案3】:

      Flask-Caching SimpleCache 不起作用。工人 > 1 Gunicorn

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

      在开发模式下一切正常,直到您使用超过 1 个工人的 gunicorn。一个可能的原因是,在开发过程中,在这种限制条件下,只有一个进程/工作人员如此奇怪SimpleCache 有效。

      我的代码是:

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

      使用Flask-Caching 的解决方案使用FileSystemCache,我的代码现在:

      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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-06-26
        • 2020-07-13
        • 2018-09-01
        • 2012-11-13
        • 1970-01-01
        • 2023-02-16
        • 2015-01-20
        相关资源
        最近更新 更多