redis-py with gevent

Joseph picture Joseph · Jun 7, 2012 · Viewed 9.4k times · Source

Can redis-py library be used with gevent ?

Does anyone have experience running redis-py library with gevent? Is there a working example?

Do they play nice together? Are there any precautions or hacks to get them to work at scale?

The question in another words:

to make redis-py connections use gevent greenlets , do I have to do anything special other than monkeypatch sockets?

from gevent import monkey

monkey.patch_all()

then use redis-py as usual?

Answer

Didier Spezia picture Didier Spezia · Jun 7, 2012

Yes, redis-py works fine with gevent.

You can check my answer to the following question: you will find an example and some remarks.

redis + gevent - Poor performance - what am I doing wrong?

The main trap is to consider that because gevent is asynchronous and the sockets are monkeypatched, the cost of roundtrips to Redis will magically disappear. This is plain wrong.

gevent is mainly interesting when several connections at the same time are used, so that the event loop system calls can be factorized. If the user code generates a lot of synchronous roundtrips to Redis on a small number of connections, it will involve latency, even if these connections are managed in an asynchronous way by gevent.

So even with gevent, pipelining must always be used to optimize the cost of communication.