Reputation: 1020
In the minimal snippet bellow with both client producer/consumers and server, I force an error on the client's send
method:
import asyncio
import websockets
async def producer(websocket):
for i in range(11):
await websocket.send(i) # Invalid type, fails silently
# await websocket.send(str(i)) # Works
async def consumer(websocket):
async for msg in websocket:
i = int(msg)
print(i)
if i == 10:
return
async def echo(websocket, path):
try:
async for msg in websocket:
await websocket.send(msg)
except websockets.exceptions.ConnectionClosedOK:
print("Client disconnect.")
async def client():
async with websockets.connect("ws://localhost:8765") as websocket:
consumer_task = consumer(websocket)
producer_task = producer(websocket)
await asyncio.wait([consumer_task, producer_task])
loop = asyncio.get_event_loop()
loop.run_until_complete(websockets.serve(echo, "localhost", 8765))
loop.run_until_complete(client())
In the invalid type case, the code hangs indefinitely, seemingly as it had "failed silently". Shouldn't the send
method raise an exception due to the invalid int
type (since it only accepts bytes
or str
)?
Upvotes: 0
Views: 771
Reputation: 1020
The problem was actually lying in the asyncio.wait
method, as hinted by Thomas' comments (thanks). Switching asyncio.wait
for the higher-level asyncio.gather
solved it.
Upvotes: 1