pallets / quart

An async Python micro framework for building web applications.
https://quart.palletsprojects.com
MIT License
2.75k stars 153 forks source link

Async for support for websockets #118

Open mozesa opened 3 years ago

mozesa commented 3 years ago

Hello @pgjones,

I just would like to keep up this topic.

There was a discussion in which Nathaniel J. Smith mentioned that async for support for websockets would be more pythonic.

Currently:

while True:
    result = await websocket.receive_json()

The more idiomatic:

async for result in websocket.receive_json():
    ...

It would be great that the async for loop supports both receive and receive_json methods.

Thanks for your help in advance.

Best Regards, András

GilbertYoder commented 1 year ago

Is this something there is interest in? One couldn't convert the receive and receive_json methods to generators w/o breaking backwards compatibility I think, but maybe additional generator functions could be added? websocket.messages() and websocket.json_messages() or something? Or maybe this would over-complicate things...

pgjones commented 1 year ago

Looks like starlette has adopted iter_text, iter_bytes and iter_json so iter_messages and iter_json I think make the most sense. However, I need to think of how to indicate the websocket has disconnected.