How to gracefully close aioredis pool in aiohttp server?

1.2k Views Asked by At

I create redis pool the following way:

async def create_redis_connection_pool(app) -> aioredis.Redis:
    redis = aioredis.from_url(
        "redis://localhost", encoding="utf-8", decode_responses=True, max_connections=10,
    )
    app["redis"] = redis
    try:
        yield
    finally:
        loop = asyncio.get_event_loop()
        await loop.create_task(app["redis"].close())

Then I use the function when I create the Aiohttp app:

def init() -> web.Application:
    app = web.Application()

    ...
    app.cleanup_ctx.append(create_redis_connection_pool)
    ...
    return app

When I start server, do at least one request which use redis pool and then do Cnrl+C I get the following warning message:

sys:1: RuntimeWarning: coroutine 'Connection.disconnect' was never awaited

How to solve the issue and gracefully close Redis connection pool? I do tests in Mac OS.

1

There are 1 best solutions below

1
On BEST ANSWER

If you're using redis==4.2.0 (from redis import asyncio as aioredis) or later,
pass close_connection_pool=True when you call .close():

await app["redis"].close(close_connection_pool=True)

Otherwise, for aioredis==2.0.1 (latest version as of this answer) or earlier,
call .connection_pool.disconnect() after .close():

await app["redis"].close()
await app["redis"].connection_pool.disconnect()

Reference: https://github.com/aio-libs/aioredis-py/pull/1256