Question

I started using Pythons async features and want to fully understand their use cases. I see webserver like FastApi or Quart which use async features. How are they working different from webservers like Flask. I learned that Webservers start a thread for each connection and the thread then handles the connection and each incoming message. Is this still the same in async servers? Is FastApi creating a thread with their own event-loop for each connection?

Was it helpful?

Solution

async functions help when you have non-cpu bound work (generally network calls of somekind). They allow a single thread to do "other stuff" (reply to another request) while waiting for the non-cpu bound work to complete.

If your webserver is single threaded then you pretty much need async if you want to serve more than one request at a time.

If your webserver is multi-threaded then its already doing the same kind of thing behind the scenes with threads, but async can still improve things by helping to identify the non-cpu bits and handle many of them with a single thread.

Its not sensible to spin up a thread for every connection, after all you still have the same cpu power and number of processors at the end of the day. Webservers will spin up a small number of threads to make best use of your computers multiple cpus/cores.

Licensed under: CC-BY-SA with attribution
scroll top