Now web-handler task is cancelled on client disconnection (normal socket closing by peer or just connection dropping by unpluging a wire etc.)
This is pretty normal behavior but it confuses people who come from world of synchronous WSGI web frameworks like flask or django.
The problem is: if web-handler writes a data to DB the task might be cancelled without waiting for transmission completion. The following snippet might be stopped on every line:
async def handler(request):
async with request.app['db'] as conn:
await conn.execute('UPDATE ...')
To make execution atomic user should spawn a task:
async def writer(db):
async with db as conn:
await conn.execute('UPDATE ...')
async def handler(request):
get_event_loop().create_task(writer(request.app['db']))
This approach is not reliable, new task is not awaited.
User need to use aiojobs:
async def handler(request):
aiojobs.spawn(writer(request.app['db']))
Or maybe not existing yet:
@aiojobs.atomic
async def handler(request):
async with request.app['db'] as conn:
await conn.execute('UPDATE ...')
Sure, I could document it in http://aiohttp.readthedocs.io/en/stable/web.html but not sure is it very visible and obvious way to protect from cancellation. Ideas?
Now web-handler task is cancelled on client disconnection (normal socket closing by peer or just connection dropping by unpluging a wire etc.)
This is pretty normal behavior but it confuses people who come from world of synchronous WSGI web frameworks like flask or django.
The problem is: if web-handler writes a data to DB the task might be cancelled without waiting for transmission completion. The following snippet might be stopped on every line:
To make execution atomic user should spawn a task:
This approach is not reliable, new task is not awaited.
User need to use aiojobs:
Or maybe not existing yet:
Sure, I could document it in http://aiohttp.readthedocs.io/en/stable/web.html but not sure is it very visible and obvious way to protect from cancellation. Ideas?