【问题标题】:Correct Way of using Redis Connection Pool in PythonPython中Redis连接池的正确使用方法
【发布时间】:2018-03-21 05:09:12
【问题描述】:

foo.pybar.py这两个不同的模块应该如何从Redis连接池中获取连接?换句话说,我们应该如何构建应用程序?

我相信我们的目标是为所有模块提供一个连接池,以便从中获取连接。

Q1:在我的示例中,两个模块是否从同一个连接池中获取连接?

Q2:是否可以在RedisClient.py创建RedisClient实例,然后将该实例导入其他2个模块?还是有更好的办法?

Q3:延迟加载conn实例变量真的​​有用吗?

RedisClient.py

import redis

class RedisClient(object):

    def __init__(self):
        self.pool = redis.ConnectionPool(host = HOST, port = PORT, password = PASSWORD)

    @property
    def conn(self):
        if not hasattr(self, '_conn'):
            self.getConnection()
        return self._conn

    def getConnection(self):
        self._conn = redis.Redis(connection_pool = self.pool)

redisClient = RedisClient()

foo.py

from RedisClient import redisClient

species = 'lion'
key = 'zoo:{0}'.format(species)
data = redisClient.conn.hmget(key, 'age', 'weight')
print(data)

bar.py

from RedisClient import redisClient

print(redisClient.conn.ping())

还是这样更好?

RedisClient.py

import redis

class RedisClient(object):

    def __init__(self):
        self.pool = redis.ConnectionPool(host = HOST, port = PORT, password = PASSWORD)

    def getConnection(self):
        return redis.Redis(connection_pool = self.pool)

redisClient = RedisClient()

foo.py

from RedisClient import redisClient

species = 'lion'
key = 'zoo:{0}'.format(species)
data = redisClient.getConnection().hmget(key, 'age', 'weight')
print(data)

bar.py

from RedisClient import redisClient

print(redisClient.getConnection().ping())

【问题讨论】:

标签: python python-3.x redis database-connection redis-py


【解决方案1】:

A1:是的,它们使用相同的连接池。

A2:这不是一个好习惯。因为您无法控制此实例的初始化。另一种方法是使用单例。

import redis


class Singleton(type):
    """
    An metaclass for singleton purpose. Every singleton class should inherit from this class by 'metaclass=Singleton'.
    """
    _instances = {}

    def __call__(cls, *args, **kwargs):
        if cls not in cls._instances:
            cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
        return cls._instances[cls]


class RedisClient(object):

    def __init__(self):
        self.pool = redis.ConnectionPool(host = HOST, port = PORT, password = PASSWORD)

    @property
    def conn(self):
        if not hasattr(self, '_conn'):
            self.getConnection()
        return self._conn

    def getConnection(self):
        self._conn = redis.Redis(connection_pool = self.pool)

那么RedisClient 将是一个单例类。不管你调用多少次client = RedisClient(),你都会得到相同的对象。

所以你可以像这样使用它:

from RedisClient import RedisClient

client = RedisClient()
species = 'lion'
key = 'zoo:{0}'.format(species)
data = client.conn.hmget(key, 'age', 'weight')
print(data)

而您第一次调用client = RedisClient() 时实际上会初始化这个实例。

或者您可能希望根据不同的参数获取不同的实例:

class Singleton(type):
    """
    An metaclass for singleton purpose. Every singleton class should inherit from this class by 'metaclass=Singleton'.
    """
    _instances = {}

    def __call__(cls, *args, **kwargs):
        key = (args, tuple(sorted(kwargs.items())))
        if cls not in cls._instances:
            cls._instances[cls] = {}
        if key not in cls._instances[cls]:
            cls._instances[cls][key] = super(Singleton, cls).__call__(*args, **kwargs)
        return cls._instances[cls][key]

【讨论】:

  • 在 Python 3 中,你使用class RedisClient(metaclass=Singleton): 还是class RedisClient(BaseClass, metaclass=Singleton):?我注意到在您的示例代码中,您没有让 RedisClient 继承 Singleton
  • 两者都是正确的。后者继承自BaseClass
  • Singleton 是一个元类,你需要metaclass=Singleton。要了解什么是元类,这是另一个大话题。
  • 能否详细说明无法控制此实例的初始化时可能遇到的问题?
  • 在你的例子中,你只是创建了一个廉价的实例,但是如果你需要初始化一个昂贵的实例呢?当您只是import RedisClient 时,您将被屏蔽一段时间。更糟糕的是,您实际上需要在RedisClient 模块中使用另一个类,而不是redisClient。所以在这种情况下,你永远不会使用redisClient,而是需要为其初始化付费。
猜你喜欢
  • 2015-10-18
  • 2015-02-09
  • 2018-11-26
  • 2019-08-26
  • 1970-01-01
  • 2015-04-04
  • 2020-06-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多