File tree Expand file tree Collapse file tree 1 file changed +7
-12
lines changed Expand file tree Collapse file tree 1 file changed +7
-12
lines changed Original file line number Diff line number Diff line change 1
1
import asyncio
2
2
import aioredis
3
3
import time
4
+ from typing import AsyncIterator
4
5
5
6
from aiohttp import web
6
7
from aiohttp_session import setup , get_session
@@ -15,23 +16,17 @@ async def handler(request: web.Request) -> web.Response:
15
16
return web .Response (text = text )
16
17
17
18
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]
19
20
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
21
25
22
26
23
27
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
-
32
28
app = web .Application ()
33
- setup (app , storage )
34
- app .on_cleanup .append (dispose_redis_pool )
29
+ app .cleanup_ctx .append (redis_pool )
35
30
app .router .add_get ('/' , handler )
36
31
return app
37
32
You can’t perform that action at this time.
0 commit comments