Is there a way to consume messages twice from a websocket connection using two separate async for loops?
Running the code below gives RuntimeError: cannot call recv while another coroutine is already waiting for the next message.
import websockets
import asyncio
async def foo(ws):
async for msg in ws:
print(f"foo: {msg}")
async def bar(ws):
async for msg in ws:
print(f"bar: {msg}")
async def main():
async with websockets.connect("wss://echo.websocket.org") as ws:
asyncio.create_task(foo(ws))
asyncio.create_task(bar(ws))
await ws.send("Hello")
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())