Flas中后台进程缓存的设置

2024-04-29 10:08:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用Flask中的simplecache框架从后台进程中设置一个缓存变量。即:

from rq import Queue
from worker import conn
from werkzeug.contrib.cache import SimpleCache
cache = SimpleCache()

app = Flask(__name__)
q = Queue(connection=conn)

# background process to be run. located in a seperate file
def test():
    for i in range(10):
        cache.set("value", i, 3600)
        time.sleep(1)

@app.route('/')
def home():
    cache.clear()
    q.empty()
    q.enqueue(test, timeout=1000)
    return jsonify({'state':"running"})

@app.route('/current_value')
def get_value():
    return jsonify({'value':cache.get("value")})

但是,这将始终返回null。我在使用Redis之前已经做过,但是SimpleCache不允许在后台进程中设置缓存吗?还是我做错了什么?在


Tags: infromtestimportappflaskcachequeue
1条回答
网友
1楼 · 发布于 2024-04-29 10:08:03

Werkzeug's SimpleCache isn't thread safe.它不打算被其他线程或进程使用,因为它不实现锁定。在

另外,文档似乎暗示了缓存存储在进程内存中,这将使主进程的缓存从辅助进程的缓存中更改变得相当困难。在

相关问题 更多 >