aio-libs / aiohttp-sse

Server-sent events support for aiohttp

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Explore better API

jettify opened this issue · comments

Our current API is following:

resp = await sse_response(request)
async with resp:
    resp.send('foo {}'.format(i))

we may rewrite to:

async with (await sse_response(request)) as resp:
    resp.send('foo {}'.format(i))

I think that is a bit ugly, it would be nice to have:

async with sse_response(request) as resp:
    resp.send('foo {}'.format(i))

It is totally achievable with hackish context manager coroutine hybrid
https://github.com/aio-libs/aiomysql/blob/4eb7e6692d68ca0a10cba7a812e75eaf83cf3dbc/aiomysql/utils.py#L31-L86
this approach used in aiohttp/aiopg/aiomysql/etc..

@ticosax @rutsky what do you think?

This option looks great to me:

async with sse_response(request) as resp:
    resp.send('foo {}'.format(i))

As for other options, is it may be useful to do something with resp here without actually doing sending?

resp = await sse_response(request)

sse_response sends pings to the browser on background, that i way we need context manager, to finalize ping task properly.

fixed in #56