0

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?

0

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.

2
  • So, frameworks like Fastapi are multithreaded Webservers with async features?
    – hadamard
    Jan 22 at 16:29
  • i dont see any claims one way or the other for fastapi
    – Ewan
    Jan 22 at 19:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.