def create_from_config(config, prefix=''):
"""Redis client instantiation from settings.
"""
settings = config.get_settings()
uri = settings[prefix + 'url']
uri = urlparse.urlparse(uri)
pool_size = int(settings[prefix + 'pool_size'])
kwargs = {
"max_connections": pool_size,
"host": uri.hostname or 'localhost',
"port": uri.port or 6379,
"password": uri.password or None,
"db": int(uri.path[1:]) if uri.path else 0
}
block_timeout = settings.get(prefix + 'pool_timeout')
if block_timeout is not None:
kwargs["timeout"] = float(block_timeout)
connection_pool = redis.BlockingConnectionPool(**kwargs)
return redis.StrictRedis(connection_pool=connection_pool)
python类BlockingConnectionPool()的实例源码
def __init__(self, config):
BaseCache.__init__(self, config)
self.key_prefix = config.get('key_prefix', '')
try:
import redis
except ImportError:
raise RuntimeError('no redis module found')
kwargs = dict((k, v) for k, v in config.items() if k not in _redis_kwargs_exclusions)
if 'socket_timeout' not in kwargs:
kwargs['socket_timeout'] = _DEFAULT_SOCKET_TIMEOUT
if 'socket_connect_timeout' not in kwargs:
kwargs['socket_connect_timeout'] = _DEFAULT_SOCKET_TIMEOUT
if 'socket_keepalive' not in kwargs:
kwargs['socket_keepalive'] = 1
if 'socket_keepalive_options' not in kwargs:
kwargs['socket_keepalive_options'] = _TCP_KEEP_ALIVE_OPTIONS
if kwargs.pop('blocking_pool', False):
if 'blocking_pool_timeout' in kwargs:
kwargs['timeout'] = kwargs.pop('blocking_pool_timeout')
else:
kwargs['timeout'] = _DEFAULT_REDIS_BLOCKING_POOL_TIMEOUT
connection_pool = redis.BlockingConnectionPool(**kwargs)
else:
connection_pool = redis.ConnectionPool(**kwargs)
self._client = redis.Redis(connection_pool=connection_pool)
def __init__(self, host, port, password=None, db_number=None,
maxconn=None, maxconn_timeout=None):
self._pool = redis.BlockingConnectionPool(
host=host,
port=port,
password=password,
db=db_number or 0,
max_connections=maxconn or 50,
timeout=maxconn_timeout or 20
)
self._client = redis.StrictRedis(
connection_pool=self._pool
)
def __init__(self, host, port, queue='queue'):
self._conn = redis.Redis(connection_pool=redis.BlockingConnectionPool(max_connections=15, host=host, port=port))
self.set_queue(queue)