quart
quart copied to clipboard
Async for support for websockets
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
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...
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.