Performance: Broadcasting to multiple clients
This concept affects the responsiveness and scalability of real-time updates sent to multiple clients, impacting interaction speed and server load.
await asyncio.gather(*(client.send_text(message) for client in clients))
for client in clients: await client.send_text(message)
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Sequential send in loop | N/A (server-side) | N/A | N/A | [X] Bad |
| Concurrent send with asyncio.gather | N/A (server-side) | N/A | N/A | [OK] Good |
async def broadcast(self, message: str):
for connection in self.active_connections:
await connection.send_text(message)
What happens if self.active_connections is empty when broadcast is called?async def broadcast(self, message: str):
for connection in self.active_connections:
connection.send_text(message)
Why does this code cause an error when broadcasting?Pass the test and this pattern moves unknown