Python aioredis.create_pool() Examples

The following are 18 code examples of aioredis.create_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: redis_storage.py    From aiohttp-session with Apache License 2.0 7 votes vote down vote up
def __init__(self, redis_pool, *, cookie_name="AIOHTTP_SESSION",
                 domain=None, max_age=None, path='/',
                 secure=None, httponly=True,
                 key_factory=lambda: uuid.uuid4().hex,
                 encoder=json.dumps, decoder=json.loads):
        super().__init__(cookie_name=cookie_name, domain=domain,
                         max_age=max_age, path=path, secure=secure,
                         httponly=httponly,
                         encoder=encoder, decoder=decoder)
        if aioredis is None:
            raise RuntimeError("Please install aioredis")
        if StrictVersion(aioredis.__version__).version < (1, 0):
            raise RuntimeError("aioredis<1.0 is not supported")
        self._key_factory = key_factory
        if isinstance(redis_pool, aioredis.pool.ConnectionsPool):
            warnings.warn(
                "using a pool created with aioredis.create_pool is deprecated"
                "please use a pool created with aioredis.create_redis_pool",
                DeprecationWarning
            )
            redis_pool = aioredis.commands.Redis(redis_pool)
        elif not isinstance(redis_pool, aioredis.commands.Redis):
            raise TypeError("Expexted aioredis.commands.Redis got {}".format(
                    type(redis_pool)))
        self._redis = redis_pool 
Example #2
Source File: redis.py    From aiocache with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _get_pool(self):
        async with self._pool_lock:
            if self._pool is None:
                kwargs = {
                    "db": self.db,
                    "password": self.password,
                    "loop": self._loop,
                    "encoding": "utf-8",
                    "minsize": self.pool_min_size,
                    "maxsize": self.pool_max_size,
                }
                if not AIOREDIS_BEFORE_ONE:
                    kwargs["create_connection_timeout"] = self.create_connection_timeout

                self._pool = await aioredis.create_pool((self.endpoint, self.port), **kwargs)

            return self._pool 
Example #3
Source File: bot.py    From xenon with GNU General Public License v3.0 6 votes vote down vote up
def start(self, *args, **kwargs):
        t = threading.Thread(target=block_check, args=(self.loop,))
        t.setDaemon(True)
        t.start()
        self.redis = aioredis.Redis(await aioredis.create_pool("redis://" + self.config.redis_host))
        # self.loop.create_task(self._shards_reader())

        return await super().start(*args, **kwargs) 
Example #4
Source File: main.py    From aiohttp-security with Apache License 2.0 6 votes vote down vote up
def init(loop):
    redis_pool = await create_pool(('localhost', 6379))
    db_engine = await create_engine(user='aiohttp_security',
                                    password='aiohttp_security',
                                    database='aiohttp_security',
                                    host='127.0.0.1')
    app = web.Application()
    app.db_engine = db_engine
    setup_session(app, RedisStorage(redis_pool))
    setup_security(app,
                   SessionIdentityPolicy(),
                   DBAuthorizationPolicy(db_engine))

    web_handlers = Web()
    web_handlers.configure(app)

    handler = app.make_handler()
    srv = await loop.create_server(handler, '127.0.0.1', 8080)
    print('Server started at http://127.0.0.1:8080')
    return srv, app, handler 
Example #5
Source File: bot.py    From modmail with GNU Affero General Public License v3.0 5 votes vote down vote up
def connect_redis(self):
        self.redis = await aioredis.create_pool("redis://localhost", minsize=5, maxsize=10, loop=self.loop, db=0)
        info = (await self.redis.execute("INFO")).decode()
        for line in info.split("\n"):
            if line.startswith("redis_version"):
                self.redis_version = line.split(":")[1]
                break 
Example #6
Source File: redis_session_test.py    From aiorest with MIT License 5 votes vote down vote up
def setUp(self):
        self.loop = asyncio.new_event_loop()
        asyncio.set_event_loop(None)

        self.redis_pool = self.loop.run_until_complete(
            aioredis.create_pool(('localhost', 6379), db=0, loop=self.loop)) 
Example #7
Source File: redis.py    From dvpwa with MIT License 5 votes vote down vote up
def _init_redis(app: Application):
    conf = app['config']['redis']
    redis = await aioredis.create_pool((conf['host'], conf['port']),
                                       db=conf['db'])
    app['redis'] = redis 
Example #8
Source File: test_footprint.py    From aiocache with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def aioredis_pool(event_loop):
    return event_loop.run_until_complete(aioredis.create_pool(("127.0.0.1", 6379), maxsize=1)) 
Example #9
Source File: redis_db.py    From nano-dpow with MIT License 5 votes vote down vote up
def __init__(self, server, loop):
        self.pool = aioredis.create_pool(
            server,
            minsize=5, maxsize=15,
            loop=loop
        ) 
Example #10
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 #11
Source File: bot.py    From modmail with GNU Affero General Public License v3.0 5 votes vote down vote up
def connect_postgres(self):
        self.pool = await asyncpg.create_pool(**self.config.database, max_size=20, command_timeout=60) 
Example #12
Source File: pool.py    From aioredis with MIT License 5 votes vote down vote up
def main():
    pool = await aioredis.create_pool(
        'redis://localhost',
        minsize=5, maxsize=10)
    with await pool as conn:    # low-level redis connection
        await conn.execute('set', 'my-key', 'value')
        val = await conn.execute('get', 'my-key')
    print('raw value:', val)
    pool.close()
    await pool.wait_closed()    # closing all open connections 
Example #13
Source File: launcher.py    From modmail with GNU Affero General Public License v3.0 5 votes vote down vote up
def event_handler(self):
        self.redis = await aioredis.create_pool("redis://localhost", minsize=1, maxsize=2)
        await self.redis.execute_pubsub("SUBSCRIBE", config.ipc_channel)
        channel = self.redis.pubsub_channels[bytes(config.ipc_channel, "utf-8")]
        while await channel.wait_message():
            payload = await channel.get_json(encoding="utf-8")
            if payload.get("scope") != "launcher" or not payload.get("action"):
                pass
            elif payload.get("action") == "restart":
                print(f"[Cluster Manager] Received signal to restart cluster {payload.get('id')}.")
                self.loop.create_task(self.get_instance(self.instances, payload.get("id")).restart())
            elif payload.get("action") == "stop":
                print(f"[Cluster Manager] Received signal to stop cluster {payload.get('id')}.")
                self.loop.create_task(self.get_instance(self.instances, payload.get("id")).stop())
            elif payload.get("action") == "start":
                print(f"[Cluster Manager] Received signal to start cluster {payload.get('id')}.")
                self.loop.create_task(self.get_instance(self.instances, payload.get("id")).start())
            elif payload.get("action") == "statuses" and payload.get("command_id"):
                statuses = {}
                for instance in self.instances:
                    statuses[str(instance.id)] = {
                        "active": instance.is_active,
                        "status": instance.status,
                        "started_at": instance.started_at,
                    }
                await self.redis.execute(
                    "PUBLISH",
                    config.ipc_channel,
                    json.dumps({"command_id": payload["command_id"], "output": statuses}),
                )
            elif payload.get("action") == "roll_restart":
                print("[Cluster Manager] Received signal to perform a rolling restart.")
                for instance in self.instances:
                    self.loop.create_task(instance.restart())
                    await asyncio.sleep(config.shards_per_cluster * 10) 
Example #14
Source File: test_redis_storage.py    From aiohttp-session with Apache License 2.0 5 votes vote down vote up
def test_redis_from_create_pool(redis_params):

    async def handler(request):
        pass

    redis = await aioredis.create_pool(**redis_params)
    with pytest.warns(DeprecationWarning):
        create_app(handler=handler, redis=redis) 
Example #15
Source File: conftest.py    From aioredis with MIT License 5 votes vote down vote up
def pool(create_pool, server, loop):
    """Returns RedisPool instance."""
    return loop.run_until_complete(create_pool(server.tcp_address)) 
Example #16
Source File: conftest.py    From aioredis with MIT License 5 votes vote down vote up
def create_pool(_closable):
    """Wrapper around aioredis.create_pool."""

    async def f(*args, **kw):
        redis = await aioredis.create_pool(*args, **kw)
        _closable(redis)
        return redis
    return f 
Example #17
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 #18
Source File: pool_pubsub.py    From aioredis with MIT License 5 votes vote down vote up
def pubsub():
    pool = await aioredis.create_pool(
        'redis://localhost',
        minsize=5, maxsize=10)

    async def reader(channel):
        while (await channel.wait_message()):
            msg = await channel.get(encoding='utf-8')
            # ... process message ...
            print("message in {}: {}".format(channel.name, msg))

            if msg == STOPWORD:
                return

    with await pool as conn:
        await conn.execute_pubsub('subscribe', 'channel:1')
        channel = conn.pubsub_channels['channel:1']
        await reader(channel)  # wait for reader to complete
        await conn.execute_pubsub('unsubscribe', 'channel:1')

    # Explicit connection usage
    conn = await pool.acquire()
    try:
        await conn.execute_pubsub('subscribe', 'channel:1')
        channel = conn.pubsub_channels['channel:1']
        await reader(channel)  # wait for reader to complete
        await conn.execute_pubsub('unsubscribe', 'channel:1')
    finally:
        pool.release(conn)

    pool.close()
    await pool.wait_closed()    # closing all open connections