Python aioredis.create_redis_pool() Examples

The following are 30 code examples of aioredis.create_redis_pool(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module aioredis , or try the search function .
Example #1
Source File: 05_pubsub.py    From aioredis with MIT License 6 votes vote down vote up
def main():
    redis = await aioredis.create_redis_pool('redis://localhost')

    ch, = await redis.psubscribe('channel:*')
    assert isinstance(ch, aioredis.Channel)

    async def reader(channel):
        async for ch, message in channel.iter():
            print("Got message in channel:", ch, ":", message)
    asyncio.get_running_loop().create_task(reader(ch))

    await redis.publish('channel:1', 'Hello')
    await redis.publish('channel:2', 'World')

    redis.close()
    await redis.wait_closed() 
Example #2
Source File: bot.py    From bot with MIT License 6 votes vote down vote up
def _create_redis_session(self) -> None:
        """
        Create the Redis connection pool, and then open the redis event gate.

        If constants.Redis.use_fakeredis is True, we'll set up a fake redis pool instead
        of attempting to communicate with a real Redis server. This is useful because it
        means contributors don't necessarily need to get Redis running locally just
        to run the bot.

        The fakeredis cache won't have persistence across restarts, but that
        usually won't matter for local bot testing.
        """
        if constants.Redis.use_fakeredis:
            log.info("Using fakeredis instead of communicating with a real Redis server.")
            self.redis_session = await fakeredis.aioredis.create_redis_pool()
        else:
            self.redis_session = await aioredis.create_redis_pool(
                address=(constants.Redis.host, constants.Redis.port),
                password=constants.Redis.password,
            )

        self.redis_closed = False
        self.redis_ready.set() 
Example #3
Source File: 02_decoding.py    From aioredis with MIT License 6 votes vote down vote up
def main():
    redis = await aioredis.create_redis_pool('redis://localhost')

    await redis.hmset_dict('hash',
                           key1='value1',
                           key2='value2',
                           key3=123)

    result = await redis.hgetall('hash', encoding='utf-8')
    assert result == {
        'key1': 'value1',
        'key2': 'value2',
        'key3': '123',  # note that Redis returns int as string
    }

    redis.close()
    await redis.wait_closed() 
Example #4
Source File: 04_pubsub.py    From aioredis with MIT License 6 votes vote down vote up
def main():
    redis = await aioredis.create_redis_pool('redis://localhost')

    ch1, ch2 = await redis.subscribe('channel:1', 'channel:2')
    assert isinstance(ch1, aioredis.Channel)
    assert isinstance(ch2, aioredis.Channel)

    async def reader(channel):
        async for message in channel.iter():
            print("Got message:", message)
    asyncio.get_running_loop().create_task(reader(ch1))
    asyncio.get_running_loop().create_task(reader(ch2))

    await redis.publish('channel:1', 'Hello')
    await redis.publish('channel:2', 'World')

    redis.close()
    await redis.wait_closed() 
Example #5
Source File: WriterConfig.py    From idataapi-transform with MIT License 6 votes vote down vote up
def get_redis_pool_cli(self):
        """
        :return: an async redis client
        """
        if self.redis_pool_cli is None:
            kwargs = {
                "db": int(self.db),
                "password": self.password,
                "encoding": self.encoding,
                "timeout": self.timeout,
                "minsize": 1,
                "maxsize": 3
            }
            if self.compress:
                del kwargs["encoding"]
            self.redis_pool_cli = await aioredis.create_redis_pool((self.host, self.port), **kwargs)
            if self.key_type == "LIST":
                if self.direction == "L":
                    self.redis_write_method = self.redis_pool_cli.lpush
                else:
                    self.redis_write_method = self.redis_pool_cli.rpush
            else:
                self.redis_write_method = self.redis_pool_cli.hset

        return self.redis_pool_cli 
Example #6
Source File: test_redis.py    From aioredlock with MIT License 6 votes vote down vote up
def test_connect_pool_not_created(self, connection, address, redis_kwargs):
        with patch('aioredlock.redis.Instance._create_redis_pool') as \
                create_redis_pool:

            fake_pool = FakePool()
            create_redis_pool.side_effect = fake_create_redis_pool(fake_pool)
            instance = Instance(connection)

            assert instance._pool is None
            pool = await instance.connect()

            create_redis_pool.assert_called_once_with(
                address, **redis_kwargs,
                minsize=1, maxsize=100)
            assert pool is fake_pool
            assert instance._pool is fake_pool 
Example #7
Source File: conftest.py    From aiohttp-session with Apache License 2.0 6 votes vote down vote up
def redis(loop, redis_params):
    pool = None

    async def start(*args, no_loop=False, **kwargs):
        nonlocal pool
        params = redis_params.copy()
        params.update(kwargs)
        useloop = None if no_loop else loop
        pool = await aioredis.create_redis_pool(loop=useloop, **params)
        return pool

    loop.run_until_complete(start())
    yield pool
    if pool is not None:
        pool.close()
        loop.run_until_complete(pool.wait_closed()) 
Example #8
Source File: utils.py    From bitcart with MIT License 5 votes vote down vote up
def make_subscriber(name):
    subscriber = await aioredis.create_redis_pool(settings.REDIS_HOST)
    res = await subscriber.subscribe(f"channel:{name}")
    channel = res[0]
    return subscriber, channel 
Example #9
Source File: redis_lib.py    From pyproxy-async with Apache License 2.0 5 votes vote down vote up
def init_pool(self, *args, **kwargs):
        size = 5
        if not self._pool:
            if not kwargs:
                kwargs = Config.REDIS
                size = Config.COROUTINE_COUNT_IP_CHECK

            self._pool = await aioredis.create_redis_pool(minsize=size + size // 2, maxsize=size * 2, *args, **kwargs) 
Example #10
Source File: settings.py    From bitcart with MIT License 5 votes vote down vote up
def init_redis():
    global redis_pool
    redis_pool = await aioredis.create_redis_pool(REDIS_HOST) 
Example #11
Source File: test_redis.py    From aioredlock with MIT License 5 votes vote down vote up
def fake_create_redis_pool(fake_pool):
    """
    Original Redis pool have magick method __await__ to create exclusive
    connection. CoroutineMock sees this method and thinks that Redis pool
    instance is awaitable and tries to await it.
    To avoit this behavior we are using this constructor with Mock.side_effect
    instead of Mock.return_value.
    """
    async def create_redis_pool(*args, **kwargs):
        return fake_pool
    return create_redis_pool 
Example #12
Source File: test_redis.py    From aioredlock with MIT License 5 votes vote down vote up
def test_connect_pool_already_created(self):

        with patch('aioredlock.redis.Instance._create_redis_pool') as \
                create_redis_pool:
            instance = Instance(('localhost', 6379))
            fake_pool = FakePool()
            instance._pool = fake_pool

            pool = await instance.connect()

            assert not create_redis_pool.called
            assert pool is fake_pool 
Example #13
Source File: test_redis.py    From aioredlock with MIT License 5 votes vote down vote up
def test_connect_pool_aioredis_instance(self):
        with patch('aioredlock.redis.Instance._create_redis_pool') as \
                create_redis_pool:
            redis_connection = await aioredis.create_redis_pool('redis://localhost')
            instance = Instance(redis_connection)

            await instance.connect()
            assert not create_redis_pool.called 
Example #14
Source File: test_redis.py    From aioredlock with MIT License 5 votes vote down vote up
def fake_instance(self):
        with patch('aioredlock.redis.Instance._create_redis_pool') as \
                create_redis_pool:
            fake_pool = FakePool()
            create_redis_pool.side_effect = fake_create_redis_pool(fake_pool)
            instance = Instance(('localhost', 6379))
            yield instance 
Example #15
Source File: redis.py    From aioredlock with MIT License 5 votes vote down vote up
def _create_redis_pool(*args, **kwargs):
        """
        Adapter to support both aioredis-0.3.0 and aioredis-1.0.0
        For aioredis-1.0.0 and later calls:
            aioredis.create_redis_pool(*args, **kwargs)
        For aioredis-0.3.0 calls:
            aioredis.create_pool(*args, **kwargs)
        """

        if StrictVersion(aioredis.__version__) >= StrictVersion('1.0.0'):  # pragma no cover
            return await aioredis.create_redis_pool(*args, **kwargs)
        else:  # pragma no cover
            return await aioredis.create_pool(*args, **kwargs) 
Example #16
Source File: conftest.py    From lightbus with Apache License 2.0 5 votes vote down vote up
def create_redis_pool(_closable, redis_server_config, loop, redis_client):
    """Wrapper around aioredis.create_redis_pool."""
    # We use the redis_client fixture to ensure that redis is flushed before each test

    async def f(*args, **kw):
        kwargs = {}
        kwargs.update(redis_server_config)
        kwargs.update(kw)
        redis = await aioredis.create_redis_pool(*args, **kwargs)
        _closable(redis)
        return redis

    return f 
Example #17
Source File: conftest.py    From lightbus with Apache License 2.0 5 votes vote down vote up
def redis_pool(create_redis_pool, loop):
    """Returns RedisPool instance."""
    return await create_redis_pool() 
Example #18
Source File: conftest.py    From lightbus with Apache License 2.0 5 votes vote down vote up
def new_redis_pool(_closable, create_redis_pool, loop):
    """Useful when you need multiple redis connections."""

    async def make_new(**kwargs):
        redis = await create_redis_pool(loop=loop, **kwargs)
        await redis.flushall()
        return redis

    return make_new 
Example #19
Source File: test_reliability_redis_connections.py    From lightbus with Apache License 2.0 5 votes vote down vote up
def test_create_and_destroy_redis_transports(
    transport_class, kwargs, redis_client, loop, redis_server_config, caplog
):
    caplog.set_level(logging.WARNING)

    for _ in range(0, 100):
        pool = await create_redis_pool(**redis_server_config, loop=loop, maxsize=1000)
        transport = transport_class(redis_pool=pool, **kwargs)
        await transport.open()
        await transport.close()

    info = await redis_client.info()

    assert int(info["stats"]["total_connections_received"]) >= 100
    assert int(info["clients"]["connected_clients"]) == 1 
Example #20
Source File: redis_database.py    From hproxy with MIT License 5 votes vote down vote up
def _db_client(self, db=None):
        client = await aioredis.create_redis_pool(
            'redis://{host}:{port}/{cur_db}'.format(host=self.host, port=self.port, cur_db=db),
            password=self.password,
            minsize=5,
            maxsize=10)
        return client 
Example #21
Source File: config.py    From dvhb-hybrid with MIT License 5 votes vote down vote up
def cleanup_ctx_redis(app, cfg_key='default', app_key='redis'):
    import aioredis
    cfg = app.context.config.redis[cfg_key].get('connection', {})
    address = cfg.get('host', 'localhost'), cfg.get('port', 6379)
    pool = await aioredis.create_redis_pool(
        address,
        db=cfg.get('db', 0),
        minsize=cfg.get('minsize', 1),
        maxsize=cfg.get('maxsize', 10),
        loop=app.loop)
    app[app_key] = pool
    yield
    pool.close()
    await pool.wait_closed() 
Example #22
Source File: redis_client.py    From tanner with GNU General Public License v3.0 5 votes vote down vote up
def get_redis_client(poolsize=None):
        redis_client = None
        try:
            host = TannerConfig.get('REDIS', 'host')
            port = TannerConfig.get('REDIS', 'port')
            if poolsize is None:
                poolsize = TannerConfig.get('REDIS', 'poolsize')
            timeout = TannerConfig.get('REDIS', 'timeout')
            redis_client = await asyncio.wait_for(aioredis.create_redis_pool(
                (host, int(port)), maxsize=int(poolsize)), timeout=int(timeout))
        except asyncio.TimeoutError as timeout_error:
            LOGGER.exception('Problem with redis connection. Please, check your redis server. %s', timeout_error)
            exit()
        return redis_client 
Example #23
Source File: redis.py    From fp-server with MIT License 5 votes vote down vote up
def init_aioredis_pool():
    keys = ('address', 'db', 'password', 'encoding')
    conf = {k: v for k, v in redis_config.items() if k in keys}
    global aioredis_pool
    aioredis_pool = await aioredis.create_redis_pool(**conf)
    logger.info('Initialized aioredis pool: %s.' % aioredis_pool) 
Example #24
Source File: GetterConfig.py    From idataapi-transform with MIT License 5 votes vote down vote up
def get_redis_pool_cli(self):
        """
        :return: an async redis client
        """
        if self.redis_pool_cli is None:
            kwargs = {
                "db": int(self.db),
                "password": self.password,
                "encoding": self.encoding,
                "timeout": self.timeout,
                "minsize": 1,
                "maxsize": 3
            }
            if self.compress:
                del kwargs["encoding"]
            self.redis_pool_cli = await aioredis.create_redis_pool((self.host, self.port), **kwargs)
            if self.key_type == "LIST":
                self.redis_read_method = self.redis_pool_cli.lrange
                self.redis_len_method = self.redis_pool_cli.llen
                self.redis_del_method = self.redis_pool_cli.delete
            else:
                self.redis_read_method = self.redis_pool_cli.hgetall
                self.redis_len_method = self.redis_pool_cli.hlen
                self.redis_del_method = self.redis_pool_cli.delete

        return self.redis_pool_cli 
Example #25
Source File: contexts.py    From website with MIT License 5 votes vote down vote up
def redis_pool(app: web.Application) -> AsyncGenerator[None, None]:
    redis = await aioredis.create_redis_pool(app["REDIS_URL"])
    _register_in_app(app, "redis", redis)
    yield
    redis.close()
    await redis.wait_closed() 
Example #26
Source File: 03_multiexec.py    From aioredis with MIT License 5 votes vote down vote up
def main():
    redis = await aioredis.create_redis_pool('redis://localhost')

    tr = redis.multi_exec()
    tr.set('key1', 'value1')
    tr.set('key2', 'value2')
    ok1, ok2 = await tr.execute()
    assert ok1
    assert ok2 
Example #27
Source File: 00_connect.py    From aioredis with MIT License 5 votes vote down vote up
def main():
    redis = await aioredis.create_redis_pool('redis://localhost')
    await redis.set('my-key', 'value')
    value = await redis.get('my-key', encoding='utf-8')
    print(value)

    redis.close()
    await redis.wait_closed() 
Example #28
Source File: commands.py    From aioredis with MIT License 5 votes vote down vote up
def redis_pool():
    # Redis client bound to pool of connections (auto-reconnecting).
    redis = await aioredis.create_redis_pool(
        'redis://localhost')
    await redis.set('my-key', 'value')
    val = await redis.get('my-key')
    print(val)

    # gracefully closing underlying connection
    redis.close()
    await redis.wait_closed() 
Example #29
Source File: integration_test.py    From aioredis with MIT License 5 votes vote down vote up
def pool_or_redis(_closable, server):
    version = tuple(map(int, aioredis.__version__.split('.')[:2]))
    if version >= (1, 0):
        factory = aioredis.create_redis_pool
    else:
        factory = aioredis.create_pool

    async def redis_factory(maxsize):
        redis = await factory(server.tcp_address,
                              minsize=1, maxsize=maxsize)
        _closable(redis)
        return redis
    return redis_factory 
Example #30
Source File: dbot.py    From avrae with GNU General Public License v3.0 5 votes vote down vote up
def setup_rdb(self):
        if config.TESTING:
            redis_url = self.credentials.test_redis_url
        else:
            redis_url = config.REDIS_URL
        return RedisIO(await aioredis.create_redis_pool(redis_url, db=config.REDIS_DB_NUM))