I'd like to set a cache variable from a background process in Flask using its SimpleCahce framework. That is:
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")})
However, this will always return null. I've done this before using Redis, but is setting cache in a background process not allowed with SimpleCache? Or am I just doing something wrong?