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
Show parent comment
1
u/Evolve-Maz Nov 07 '25
I had trouble fully understanding async for handling multiple connections, but once you understand the magic of the python yield keyword it falls into place quite nicely.
Here's a really helpful talk, look up "david beazley python concurrency from the ground up" on YouTube (can't link it at the moment). He highlights how the yield keyword in python lets you do some magic, and how that magic allows you to write your own scheduler behind the scenes. It helped me understand exactly how async/await was doing things, and also let me know why there are certain foot guns with it.