I am working on a small service using Gunicorn and Flask (Python 3.6).The pseudocode below shows roughly the behavior I want. There are a lot of serialized foo
objects, and I want to hold as many of these in memory as possible and delete them on a LRU basis.
cache = Cache()
@app.route('/')
def foobar():
name = request.args['name']
foo = cache.get(name)
if foo is None:
foo = load_foo(name)
cache.add(foo)
return foo.bar()
The problem I am having is I do not know how to share this cache between Gunicorn workers. I'm working with limited memory and don't want to be holding duplicate objects. Certain objects will be used very often and some probably never, so I think it really makes sense to hold them in memory.
This is just something that will only be taking requests from another application (both running on the same server), I just wanted to keep this code separate. Am I going the completely wrong direction by even using Gunicorn in the first place?