Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly handle Redis connections with Python / Python RQ?

What is the best pattern to handle Redis connections (both for interacting with Redis directly and indirectly through Python-RQ)?

Generally, database connections need to be closed / returned to a pool when done, but I don't see how to do that with redis-py. That makes me wonder if I'm doing it the wrong way.

Also, I have seen some performance dips when enqueuing jobs to RQ, which I've been told might relate to poor connection usage / reuse.

Basically, I'm interested in knowing the correct pattern, so I can either verify or correct what we have in our application.

Thanks a lot! If there's more information that would be helpful, let me know.

like image 826
Juan Carlos Coto Avatar asked Mar 13 '13 17:03

Juan Carlos Coto


People also ask

How does RQ work?

With RQ, you don't have to set up any queues upfront, and you don't have to specify any channels, exchanges, routing rules, or whatnot. You can just put jobs onto any queue you want. As soon as you enqueue a job to a queue that does not exist yet, it is created on the fly.

What is RQ in Python?

RQ, also known as Redis Queue, is a Python library that allows developers to enqueue jobs to be processed in the background with workers. The RQ workers will be called when it's time to execute the queue in the background.


1 Answers

Behind the scenes, redis-py uses a connection pool to manage connections to a Redis server. By default, each Redis instance you create will in turn create its own connection pool. You can override this behavior and use an existing connection pool by passing an already created connection pool instance to the connection_pool argument of the Redis class. You may choose to do this in order to implement client side sharding or have finer grain control of how connections are managed

pool = redis.ConnectionPool(host='localhost', port=6379, db=0)
r = redis.Redis(connection_pool=pool)
like image 153
mmenafra Avatar answered Sep 30 '22 17:09

mmenafra