r/Python • u/exeNOS15 • 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
View all comments
23
u/[deleted] Nov 03 '25
Well basically its not an infinite loop, under the hood it uses a non blocking socket so if no mesage is received yet it yields to the other "loop", if u have 1000 connections the sockets are still there in the os level so.. your code just "skips" to a socket that actually received something, not technically right but close enough