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?

asked Jan 22, 2021 at 15:44

1 Answer 1

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.

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.