def test_close_pool_in_main(self, max_connections): """ A child process that uses the same pool as its parent isn't affected when the parent disconnects all connections within the pool. """ pool = ConnectionPool.from_url('redis://localhost', max_connections=max_connections) conn = pool.get_connection('ping') assert conn.send_command('ping') is None assert conn.read_response() == b'PONG' def target(pool, disconnect_event): conn = pool.get_connection('ping') with exit_callback(pool.release, conn): assert conn.send_command('ping') is None assert conn.read_response() == b'PONG' disconnect_event.wait() assert conn.send_command('ping') is None assert conn.read_response() == b'PONG' ev = multiprocessing.Event() proc = multiprocessing.Process(target=target, args=(pool, ev)) proc.start() pool.disconnect() ev.set() proc.join(3) assert proc.exitcode is 0
def test_close_pool_in_main(self, max_connections, master_host): """ A child process that uses the same pool as its parent isn't affected when the parent disconnects all connections within the pool. """ pool = ConnectionPool.from_url( f"redis://{master_host[0]}:{master_host[1]}", max_connections=max_connections, ) conn = pool.get_connection("ping") assert conn.send_command("ping") is None assert conn.read_response() == b"PONG" def target(pool, disconnect_event): conn = pool.get_connection("ping") with exit_callback(pool.release, conn): assert conn.send_command("ping") is None assert conn.read_response() == b"PONG" disconnect_event.wait() assert conn.send_command("ping") is None assert conn.read_response() == b"PONG" ev = multiprocessing.Event() proc = multiprocessing.Process(target=target, args=(pool, ev)) proc.start() pool.disconnect() ev.set() proc.join(3) assert proc.exitcode == 0
def test_close_pool_in_main(self, max_connections): """ A child process that uses the same pool as its parent isn't affected when the parent disconnects all connections within the pool. """ pool = ConnectionPool.from_url('redis://localhost', max_connections=max_connections) conn = pool.get_connection('ping') assert conn.send_command('ping') is None assert conn.read_response() == b'PONG' def target(pool, disconnect_event): conn = pool.get_connection('ping') with exit_callback(pool.release, conn): assert conn.send_command('ping') is None assert conn.read_response() == b'PONG' disconnect_event.wait() assert conn.send_command('ping') is None assert conn.read_response() == b'PONG' ev = multiprocessing.Event() proc = multiprocessing.Process(target=target, args=(pool, ev)) proc.start() pool.disconnect() ev.set() proc.join(3) assert proc.exitcode is 0
def test_pool(self, max_connections, master_host): """ A child will create its own connections when using a pool created by a parent. """ pool = ConnectionPool.from_url( f"redis://{master_host[0]}:{master_host[1]}", max_connections=max_connections, ) conn = pool.get_connection("ping") main_conn_pid = conn.pid with exit_callback(pool.release, conn): conn.send_command("ping") assert conn.read_response() == b"PONG" def target(pool): with exit_callback(pool.disconnect): conn = pool.get_connection("ping") assert conn.pid != main_conn_pid with exit_callback(pool.release, conn): assert conn.send_command("ping") is None assert conn.read_response() == b"PONG" proc = multiprocessing.Process(target=target, args=(pool, )) proc.start() proc.join(3) assert proc.exitcode == 0 # Check that connection is still alive after fork process has exited # and disconnected the connections in its pool conn = pool.get_connection("ping") with exit_callback(pool.release, conn): assert conn.send_command("ping") is None assert conn.read_response() == b"PONG"
def test_pool(self, max_connections): """ A child will create its own connections when using a pool created by a parent. """ pool = ConnectionPool.from_url('redis://localhost', max_connections=max_connections) conn = pool.get_connection('ping') main_conn_pid = conn.pid with exit_callback(pool.release, conn): conn.send_command('ping') assert conn.read_response() == b'PONG' def target(pool): with exit_callback(pool.disconnect): conn = pool.get_connection('ping') assert conn.pid != main_conn_pid with exit_callback(pool.release, conn): assert conn.send_command('ping') is None assert conn.read_response() == b'PONG' proc = multiprocessing.Process(target=target, args=(pool,)) proc.start() proc.join(3) assert proc.exitcode is 0 # Check that connection is still alive after fork process has exited # and disconnected the connections in its pool conn = pool.get_connection('ping') with exit_callback(pool.release, conn): assert conn.send_command('ping') is None assert conn.read_response() == b'PONG'
def test_pool(self, max_connections): """ A child will create its own connections when using a pool created by a parent. """ pool = ConnectionPool.from_url('redis://localhost', max_connections=max_connections) conn = pool.get_connection('ping') main_conn_pid = conn.pid with exit_callback(pool.release, conn): conn.send_command('ping') assert conn.read_response() == b'PONG' def target(pool): with exit_callback(pool.disconnect): conn = pool.get_connection('ping') assert conn.pid != main_conn_pid with exit_callback(pool.release, conn): assert conn.send_command('ping') is None assert conn.read_response() == b'PONG' proc = multiprocessing.Process(target=target, args=(pool, )) proc.start() proc.join(3) assert proc.exitcode is 0 # Check that connection is still alive after fork process has exited # and disconnected the connections in its pool conn = pool.get_connection('ping') with exit_callback(pool.release, conn): assert conn.send_command('ping') is None assert conn.read_response() == b'PONG'
def from_url(cls, url, db=None, **kwargs): if 'max_connections' not in kwargs: kwargs['max_connections'] = 100 kwargs.pop('timeout', None) connection_pool = ConnectionPool.from_url(url, db=db, **kwargs) return cls(connection_pool=connection_pool)
def __init__(self, hosts, **kwargs): host = hosts[0] if isinstance(host, str): self.pool = ConnectionPool.from_url(host, **kwargs) else: db = host.get('db', 0) self.pool = ConnectionPool(host=host['host'], port=host['port'], db=db, **kwargs)
def from_url(cls, url, **kwargs): """ Return a Disque client object configured from the given URL. For example:: disque://[:password]@localhost:6379 unix://[:password]@/path/to/socket.sock Any additional querystring arguments and keyword arguments will be passed along to the ConnectionPool class's initializer. In the case of conflicting arguments, querystring arguments always win. """ connection_pool = ConnectionPool.from_url(url, **kwargs) return cls(connection_pool=connection_pool)
def from_url(cls, url, **kwargs): """ Return a Disque client object configured from the given URL. For example:: disque://[:password]@localhost:6379 unix://[:password]@/path/to/socket.sock Any additional querystring arguments and keyword arguments will be passed along to the ConnectionPool class's initializer. In the case of conflicting arguments, querystring arguments always win. """ connection_pool = ConnectionPool.from_url(url, **kwargs) return cls(connection_pool=connection_pool)
def init_pools(self, hosts, **kwargs): # Ensure consistency of db value kwargs.update({'db': 0}) for host in hosts: if isinstance(host, str): addr = self.parse_addr(host) pool = ConnectionPool.from_url(host, **kwargs) else: addr = '{}:{}'.format(host['host'], host['port']) pool = ConnectionPool(host=host['host'], port=host['port'], **kwargs) self.cluster_pools[addr] = pool self.reset_slots(**kwargs)
def main(argv=sys.argv): if len(argv) < 2: usage(argv) config_uri = argv[1] options = parse_vars(argv[2:]) setup_logging(config_uri) settings = get_appsettings(config_uri, options=options) logging.getLogger("c2corg_api").setLevel(logging.INFO) redis_url = "{0}?db={1}".format(settings["redis.url"], settings["redis.db_cache"]) log.info("Cache Redis: {0}".format(redis_url)) # we don't really need a connection pool here, but the `from_url` # function is convenient redis_pool = ConnectionPool.from_url(redis_url, max_connections=1) # remove all keys from the database r = Redis(connection_pool=redis_pool) r.flushdb() log.info("Flushed cache")
def init_app(self, app): config = Config(app) redis.connection_pool = ConnectionPool.from_url( config.value('REDIS_URL')) self.register_blueprint(app) if config.value('TOKEN_URL') is not False: app.add_url_rule(config.url_rule_for('TOKEN_URL'), view_func=views.access_token, methods=['POST']) if config.value('MANAGEMENT_URL') is not False: app.add_url_rule(config.url_rule_for('MANAGEMENT_URL'), view_func=views.management, methods=['POST', 'GET']) mongo.init_app(app) self.mongo = mongo oauth.init_app(app) oauth._validator = MyRequestValidator()
def main(argv=sys.argv): if len(argv) < 2: usage(argv) config_uri = argv[1] options = parse_vars(argv[2:]) setup_logging(config_uri) settings = get_appsettings(config_uri, options=options) logging.getLogger('c2corg_ui').setLevel(logging.INFO) log.info('Cache Redis: {0}'.format(settings['redis.url'])) # we don't really need a connection pool here, but the `from_url` # function is convenient redis_pool = ConnectionPool.from_url( settings['redis.url'], max_connections=1) # remove all keys from the database r = Redis(connection_pool=redis_pool) r.flushdb() log.info('Flushed cache')
def main(argv=sys.argv): if len(argv) < 2: usage(argv) config_uri = argv[1] options = parse_vars(argv[2:]) setup_logging(config_uri) settings = get_appsettings(config_uri, options=options) logging.getLogger('c2corg_ui').setLevel(logging.INFO) log.info('Cache Redis: {0}'.format(settings['redis.url'])) # we don't really need a connection pool here, but the `from_url` # function is convenient redis_pool = ConnectionPool.from_url( settings['redis.url'], max_connections=1) # remove all keys from the database r = Redis(connection_pool=redis_pool) r.flushdb() log.info('Flushed cache')
def from_url(cls, url, db=None, **kwargs): """ Return a Redis client object configured from the given URL. For example:: redis://[:password]@localhost:6379/0 unix://[:password]@/path/to/socket.sock?db=0 There are several ways to specify a database number. The parse function will return the first specified option: 1. A ``db`` querystring option, e.g. redis://localhost?db=0 2. If using the redis:// scheme, the path argument of the url, e.g. redis://localhost/0 3. The ``db`` argument to this function. If none of these options are specified, db=0 is used. Any additional querystring arguments and keyword arguments will be passed along to the ConnectionPool class's initializer. In the case of conflicting arguments, querystring arguments always win. """ connection_pool = ConnectionPool.from_url(url, db=db, **kwargs) return cls(connection_pool=connection_pool)
def init_app(self, app): config = Config(app) redis.connection_pool = ConnectionPool.from_url( config.value('REDIS_URL')) self.register_blueprint(app) if config.value('TOKEN_URL') is not False: app.add_url_rule( config.url_rule_for('TOKEN_URL'), view_func=views.access_token, methods=['POST'] ) if config.value('MANAGEMENT_URL') is not False: app.add_url_rule( config.url_rule_for('MANAGEMENT_URL'), view_func=views.management, methods=['POST', 'GET'] ) mongo.init_app(app, config_prefix='SENTINEL_MONGO') self.mongo = mongo oauth.init_app(app) oauth._validator = MyRequestValidator()
from redis.connection import ConnectionPool from redis.client import StrictRedis from .config import get_config pool = ConnectionPool.from_url(get_config()["REDIS_URL"]) redis_store = StrictRedis(connection_pool=pool)
def from_url(cls, url, db=None, namespace='', **kwargs): connection_pool = ConnectionPool.from_url(url, db=db, **kwargs) return cls(connection_pool=connection_pool, namespace=namespace)
from flask import Flask from flask.ext.sentinel import ResourceOwnerPasswordCredentials, oauth from flask.ext.sentinel.core import redis from redis.connection import ConnectionPool app = Flask(__name__) app.config.from_object('setup') # workaround on a redis connection bug in flask-sentinel v0.0.2 redis.connection_pool=ConnectionPool.from_url(app.config['SENTINEL_REDIS_URL']) ResourceOwnerPasswordCredentials(app) @app.route('/endpoint') @oauth.require_oauth() def restricted_access(): return "You made it through and accessed the protected resource!" if __name__ == '__main__': app.run(ssl_context='adhoc')
from log_config import logging_config, dictConfig dictConfig(logging_config) from redis.connection import ConnectionPool from huey import RedisHuey from settings import REDIS_URL redis_pool = ConnectionPool.from_url(REDIS_URL) docker_huey = RedisHuey('docker_huey', connection_pool=redis_pool)
def __getitem__(self, server): if server not in self: self.__pools[server] = ConnectionPool.from_url(url=server, parser_class=Parser) return self.__pools[server]