python连接redis 多个db,在Redis Python中创建和管理多个连接

I am using Redis to store two databases : 0 and 1 via the Redis-py client library. I would like to create two connections for each database. Currently, I am doing this :

>>> connection0 = redis.Connection(host = 'localhost', port = 6379, db = 0)

>>> connection1 = redis.Connection(host = 'localhost', port = 6379, db = 1)

>>> connection0.connect()

However, I don't seem to find a way to create a Redis object from the connection.

>>> store0 = redis.Redis(connection0)

>>> store0.info()

Traceback (most recent call last):

File "", line 1, in

File "/Library/Frameworks/Python.framework/Versions/7.0/lib/python2.7/site-packages/redis-2.4.11-py2.7.egg/redis/client.py", line 341, in info

return self.execute_command('INFO')

File "/Library/Frameworks/Python.framework/Versions/7.0/lib/python2.7/site-packages/redis-2.4.11-py2.7.egg/redis/client.py", line 278, in execute_command

connection.send_command(*args)

File "/Library/Frameworks/Python.framework/Versions/7.0/lib/python2.7/site-packages/redis-2.4.11-py2.7.egg/redis/connection.py", line 258, in send_command

self.send_packed_command(self.pack_command(*args))

File "/Library/Frameworks/Python.framework/Versions/7.0/lib/python2.7/site-packages/redis-2.4.11-py2.7.egg/redis/connection.py", line 241, in send_packed_command

self.connect()

File "/Library/Frameworks/Python.framework/Versions/7.0/lib/python2.7/site-packages/redis-2.4.11-py2.7.egg/redis/connection.py", line 187, in connect

sock = self._connect()

File "/Library/Frameworks/Python.framework/Versions/7.0/lib/python2.7/site-packages/redis-2.4.11-py2.7.egg/redis/connection.py", line 198, in _connect

sock.connect((self.host, self.port))

File "/Library/Frameworks/Python.framework/Versions/7.0/lib/python2.7/socket.py", line 224, in meth

return getattr(self._sock,name)(*args)

TypeError: coercing to Unicode: need string or buffer, Connection found

Am I making a rookie mistake here?

解决方案

You really shouldn't create connections like that. Let me quote the redis-py documentation.

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.StrictRedis(connection_pool=pool)

You cannot specify a single connection to be used with the library. Each Redis instance will have its own connection pool. When execute_command() is called, it will pop a connection from a the pool(or open a new one) and use that connection. If you only want your client to have max one connection at a time, set max_connections to 1.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值