Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I share a cache between Gunicorn workers?

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?

like image 233
user910210 Avatar asked Mar 29 '17 15:03

user910210


People also ask

Do Gunicorn workers shared memory?

Gunicorn also allows for each of the workers to have multiple threads. In this case, the Python application is loaded once per worker, and each of the threads spawned by the same worker shares the same memory space.

How many workers can Gunicorn handle?

Gunicorn should only need 4-12 worker processes to handle hundreds or thousands of requests per second.

Does Gunicorn use multiprocessing?

Multiprocessing with Gunicorn It provides 2 URL: the first one (/gen) creates 5 new instances of the C class. The second (/test) returns the ID of the current process and the number of instances in the quadstore.

Is Gunicorn single threaded?

threads. Default is 1. This tells number of threads in each worker process. This means that each gunicorn worker is single threaded and isn't multithreaded.


1 Answers

I don't see anything wrong with using Gunicorn, but it's probably not necessary to think about scaling horizontally unless you are close to putting this into production. Anyway, I'd recommend using a separate service as a cache, rather than having one in python memory. That way, each worker can open a connection to the cache as needed. Redis is a popular option, but you may have to do some data manipulation to store the data, e.g. store the data as a JSON string rather than a python object. Redis can act as a LRU cache by configuring it: https://redis.io/topics/lru-cache

like image 175
Max Paymar Avatar answered Sep 25 '22 08:09

Max Paymar