Skip to content

Commit 6240718

Browse files
Use cleanup_ctx for redis demo (#609)
1 parent a5c7814 commit 6240718

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

demo/redis_storage.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import asyncio
22
import aioredis
33
import time
4+
from typing import AsyncIterator
45

56
from aiohttp import web
67
from aiohttp_session import setup, get_session
@@ -15,23 +16,17 @@ async def handler(request: web.Request) -> web.Response:
1516
return web.Response(text=text)
1617

1718

18-
async def make_redis_pool() -> aioredis.commands.Redis: # type: ignore[no-any-unimported]
19+
async def redis_pool(app: web.Application) -> AsyncIterator[aioredis.commands.Redis]: # type: ignore[no-any-unimported]
1920
redis_address = ('127.0.0.1', '6379')
20-
return await aioredis.create_redis_pool(redis_address, timeout=1)
21+
async with await aioredis.create_redis_pool(redis_address, timeout=1) as redis:
22+
storage = RedisStorage(redis)
23+
setup(app, storage)
24+
yield
2125

2226

2327
def make_app() -> web.Application:
24-
loop = asyncio.get_event_loop()
25-
redis_pool = loop.run_until_complete(make_redis_pool())
26-
storage = RedisStorage(redis_pool)
27-
28-
async def dispose_redis_pool(app: web.Application) -> None:
29-
redis_pool.close()
30-
await redis_pool.wait_closed()
31-
3228
app = web.Application()
33-
setup(app, storage)
34-
app.on_cleanup.append(dispose_redis_pool)
29+
app.cleanup_ctx.append(redis_pool)
3530
app.router.add_get('/', handler)
3631
return app
3732

0 commit comments

Comments
 (0)