2012-01-24 7 views
8

私はRedisを使用して2つのデータベースを保存しています:Redis-pyクライアントライブラリ経由で0と1です。私は各データベースの2つの接続を作成したいと思います。現在、私はこれをやっている:Redis Pythonでの複数の接続の作成と管理

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

しかし、私は接続からRedisのオブジェクトを作成する方法を見つけるように見えることはありません。

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

私はここでルーキーミスをしていますか?

答えて

10

あなたは本当にそのような接続を作成すべきではありません。 redis-pyのドキュメントを引用してみましょう。

redis-pyは接続プールを使用して、のRedisサーバーへの接続を管理します。デフォルトでは、作成した各Redisインスタンス は、独自の接続プールを作成します。 この動作を上書きして、すでに作成された接続プールインスタンス をRedisクラスのconnection_pool 引数に渡すことで、既存の接続プールを使用できます。 クライアントサイドシャーディングを実装するか、細かい粒度制御を使用して 接続を管理することができます。

>>> pool = redis.ConnectionPool(host='localhost', port=6379, db=0) 
>>> r = redis.StrictRedis(connection_pool=pool) 

あなたはライブラリを使用する単一の接続を指定することはできません。各Redisインスタンスには独自の接続プールがあります。 execute_command()が呼び出されると、プールからの接続がポップされ(または新しい接続がオープンされ)、その接続が使用されます。一度に1つのクライアントに最大接続数を1つだけ設定する場合は、max_connectionsを1に設定します。

関連する問題