What is the purpose of Uvicorn?
Asked Answered
T

1

22

I supposed to work with FastAPI. I was taught that FastAPI is used with Uvicorn. What exactly is Uvicorn. I don't know what uvicorn is doing with FastAPI exactly. Can anyone explain?

Teaching answered 11/3, 2022 at 8:54 Comment(0)
A
26

uvicorn is an ASGI (async server gateway interface) compatible web server. It's (simplified) the binding element that handles the web connections from the browser or api client and then allows FastAPI to serve the actual request.

uvicorn listens on a socket, receives the connection, does a bit processing and hands the request over to FastAPI, according to the ASGI interface.

You can read more about what the ASGI specification is, why ASGI was needed and other ASGI implementations on Uvicorn's homepage.

Alarice answered 11/3, 2022 at 9:11 Comment(8)
Can uvicorn be used as a production server ?Spokane
@Spokane Usually you'll run gunicorn with the UvicornWorker in that case, as that allows you to properly spawn many workers and scale your deployment properly. This is also what's suggested in uvicorn's deployment guide: uvicorn.org/deploymentAlarice
Is it the same as http-server in Node.js?Homerus
@MaulanaAdamSahid while I'm not deeply familiar with http-server, if my memory serves me correct, it only serves static files. The ASGI protocol is meant for dynamic web applications, not (only) static files.Alarice
is uvicorn the most popular web framework for asgi?Buroker
@Buroker While not related to this question, uvicorn isn't really an application framework, it's more of a webserver or application server that is used by other frameworks (FastAPI, Starlette, etc. - anything compatible with ASGI).Alarice
@Alarice yeah, as im reading up on it i realized this. i looking to move to a faster ASGI framework from Flask. looks like Blacksheep and FastApi are possible good choices.Buroker
It's usually not the framework that is the limiting factor, but what you're doing inside the framework - Flask can be more than quick enough. But I'm happy with both Flask and FastAPI, and I do like how FastAPI allows for compositing dependencies required for endpoints. async is its own can of worms as it requires everything to be async compatible in your critical paths to have any real performance gain. You're also mostly going to use gunicorn with the uvicorn worker in production.Alarice

© 2022 - 2024 — McMap. All rights reserved.