Setup aiohttp web app with Session Middleware to use Redis Storage and run under Gunicorn.
For using Redis Storage in aiohttp_session
you need to create Redis connection pool, which obviously requires you to have
awaitable call, so you need to put middleware setup of your
inside the coroutine, like:
async def create_app(): app = web.Application() pool = await create_pool(('127.0.0.1', 6379), db=0) aiohttp_session.setup(app, RedisStorage(pool)) return app
This requires call create_app coroutine inside of asyncio event loop as:
loop = asyncio.get_event_loop() app = loop.run_until_complete(create_app())
But when you serve aiohttp app with Gunicorn, you might don't want to have this additional loop (and it may not work at all).
In case if written above is the problem for you, you can easily fix it, by
session_middleware into your own session middleware
factory. And as a middleware factory is a coroutine by design, you're able
to make a Redis connection pool inside your factory for free.
def create_app(): return web.Application(middlewares=[ session_middleware_factory, ]) async def session_middleware_factory(app, handler): pool = await create_pool(('127.0.0.1', 6379), db=0) return await session_middleware(RedisStorage(pool))(app, handler) app = create_app()
Whole code for the solution available in
$ make run
This requires bootstrapper to be installed in the system with Python 3.5+
I'm @playpausenstop on Twitter and link to my personal site and more contacts available in GitHub profile.