在Redis Python中创建和管理多个连接 [英] Creating and managing multiple connections in Redis Python

查看:369
本文介绍了在Redis Python中创建和管理多个连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Redis通过 Redis-py 客户端存储两个数据库:0和1图书馆。我想为每个数据库创建两个连接。目前,我正在这样做:

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()

但是,我似乎没有找到创建Redis对象的方法

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 "<stdin>", line 1, in <module>
  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?

推荐答案

您真的不应该那样创建连接。让我引用redis-py文档。

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


在后台,redis-py使用连接池管理与
的连接Redis服务器。默认情况下,您
创建的每个Redis实例将依次创建自己的连接池。您可以通过将
已经创建的连接池实例传递给Redis类的connection_pool
参数来覆盖
此行为并使用现有的连接池。您可以选择这样做,以便
实施客户端分片或更好地控制
连接的管理方式。

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)

您不能指定单个连接用于图书馆。每个Redis实例将具有其自己的连接池。调用execute_command()时,它将从池中弹出一个连接(或打开一个新的连接)并使用该连接。如果只希望客户端一次最多建立一个连接,请将max_connections设置为1。

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.

这篇关于在Redis Python中创建和管理多个连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆