Skip to content

Instantly share code, notes, and snippets.

@tawateer
Last active August 29, 2015 14:27
Show Gist options
  • Save tawateer/2ecdffa8c78301703725 to your computer and use it in GitHub Desktop.
Save tawateer/2ecdffa8c78301703725 to your computer and use it in GitHub Desktop.
从 redis 多连接池中生成对象
#-*- coding: utf-8 -*-
import redis
REDIS_HOST = "localhost"
REDIS_PORT = 6379
REDIS_DB1 = 0
REDIS_DB2 = 1
REDIS_DB3 = 2
def singleton(cls):
instances = {}
def _singleton(*args, **kw):
if cls not in instances:
instances[cls] = cls(*args, **kw)
return instances[cls]
return _singleton
@singleton
class RedisClient(object):
conn_pool = dict()
for redis_db in (REDIS_DB1, REDIS_DB2, REDIS_DB3):
conn_pool[redis_db] = redis.connection.BlockingConnectionPool(
host=REDIS_HOST, port=REDIS_PORT,
db=redis_db, timeout=60)
@classmethod
def get(cls, db):
return redis.client.Redis(connection_pool=cls.conn_pool[db])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment