I want to be able to use a WebSocket in Quart to receive any messages that are sent, and send any messages that I may need to send. There's no guarantee that messages will alternate between sending and receiving.
As an example, Quart's tutorial page on WebSockets features the following snippet:
@app.websocket('/api/v2/ws')
@collect_websocket
async def ws(queue):
while True:
data = await queue.get()
await websocket.send(data)
Somehow, I want to modify the code in the while True
loop so that I can check if there is any data to be received, but if there isn't, I will instead check the queue.
I would like to be able to await receiving on the socket only if there is something to receive (which maybe could be achieved if there were a timeout
parameter in the receive
method), but that is not an option.
So, how can I await
a WebSocket for updates while also await
ing something else for updates?