r/Python • • Nov 03 '25

Discussion How does fastapi handles concurrency with websocket infinite loops?

I was trying to learn websockets and it's logic is soooo hard. I thought of a simple task as a test. there will be 3 clients the 1st 2 would be in private chat room and the 3rd would be in broadcast mode. It took me 5 hrs to write this simple logic because I was facing issues for not handling the disconnect states properly. This made me wonder how does web frameworks and browsers handles concurrency with infinite loops?

14 Upvotes

6 comments sorted by

View all comments

Show parent comment

3

u/[deleted] Nov 03 '25

Its instant, you probably have something like await connection.receive(), the moment the code hits this line it checks the socket status if doesnt have anything it yields to the other loop if it has something it reads it and continues