aio-libs / aiohttp-sse

Server-sent events support for aiohttp

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How it exits from handler?

qerdcv opened this issue · comments

By basic example

async def sse(request: web.Request):
    id = request.match_info['id']

    async with sse_response(request) as resp:
        while True:
            print('ping')
            data = 'Server Time : {}'.format(datetime.now())
            await resp.send(data)
            await asyncio.sleep(1)

    print('after_loop')
    return resp

My purpose is to handle close event from client.

And I not fully understand how it exits from the handler, if client is closed it connection, after_loop is not printing, and don't see any traceback with error.

Sorry if question is obvious

I suspect this goes back to aiohttp behaviour, there is nothing special that aiohttp-sse is doing.
i.e. When the connection is dropped by a client, aiohttp will cancel the task, thus the code will never reach the end of the loop.

I'm not sure why it's actually returning the resp at the end if the code will never be run. We can probably just remove that from the example.

Looks like the loop wasn't infinite when the example was originally written: 44889b8#diff-7b3ed02bc73dc06b7db906cf97aa91dec2b2eb21f2d92bc5caa761df5bbc168fR94

So, just a case of refactoring messing up the example.

Thanks a lot!