Reputation: 2298
Here is what I am doing. redis-py blows up when i do this.
Python 2.7.3 (default, Aug 1 2012, 05:16:07)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import redis
>>> p = redis.ConnectionPool()
>>> r = redis.Redis(connection_pool=p)
>>> p.release(r)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/redis/connection.py", line 355, in release
if connection.pid == self.pid:
AttributeError: 'Redis' object has no attribute 'pid'
>>>
Upvotes: 4
Views: 6451
Reputation: 2298
OK since nobody responded to this question, I took some time and went through the redis-py source code (use the source luke!) and here is what I found. I will share it here in case someone else faces the same issue in the future.
The Redis() object returned by
r = redis.Redis(connection_pool=p)
does not represent a redis connection. It is just a Python object on which the redis API has been proxied. When we make a call to a specific API, say
r.keys()the internal proxy uses the connection pool passed to ask for a connection before it creates the command and payload and fires against the connection obtained from the pool. It then returns the connection back to the pool. All these actions are transparent to the API user.
The bottom line is that you can create any number of redis.Redis() objects in the code and just discard them. The Python GC will handle object cleanup while the connection_pool is managed outside of the Redis() object.
Upvotes: 23