Modern APIs with FastAPI and Python Transcripts
Chapter: Modern language foundations
Lecture: WSGI and ASGI servers

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Have you ever wondered how you can write a web application in one Python framework, whether that's Flask or Django or Pyramid or even FastAPI?
0:10 And then you get to choose where you run it. You could put it on Heroku and they run it somehow. Who knows? You could run it under Gunicorn.
0:17 You could run it under micro WSGI, UWSGI. All of these different options are available to us because these Python Web
0:24 frameworks plug into a general hosting architecture. For the longest time,
0:29 that architecture was called "Web Service Gateway Interface" or WSGI, and the WSGI servers, well, those are the ones I named, Gunicorn, Micro WSGI,
0:38 a whole bunch of other ones. And they have a specific implementation that expects you pass function, that request is processed, the function is called,
0:48 the return value is then returned, and then the server takes the next response and goes with it.
0:53 So here literally is what the definition of a WSGI server is. It basically has this single request,
1:00 and somehow that plugs into the Web framework, like Flask or FastAPI or whatever, starts a response, Then it gets into Flask and does all its work,
1:07 and then it returns that response over the network. But what you don't see is any mechanism to handle concurrent things,
1:15 to begin with some sort of async call and then respond to it and so on. So because these servers were built in a time when Python literally did not
1:24 have support for async and await, and asyncio, they obviously didn't factor that in. And if you change it, it's a breaking change,
1:32 right? So we don't want to change how WSGI works. So in order for us to run a asynchronous Web framework, like FastAPI,
1:40 to full advantage, we have to use what's called an ASGI, or "Asynchronous Server Gateway Interface". Now there's some servers that support this, right.
1:49 Uvicorn is one of them. There's others as well, and they have an implementation that looks a lot like this,
1:56 and here's a little arbitrary implementation that I threw in here. Maybe we're gonna call receive and give it the scope,
2:01 but we're going to await that and potentially handle other calls while this one's being handled
2:05 then we're gonna, who knows what other middleware we're applying whatever, and then we're gonna work on the sending data back,
2:12 okay? Because ASGI fundamentally bakes in asynchronous capabilities, we can do many requests at the same time. We have 100 out requests,
2:23 all waiting on a database or some external web service or micro service or something,
2:26 we can handle another request because we're actually not doing anything at all. We're just awaiting them, right? So that's really, really awesome.
2:32 The reason I bring this up is many of these asynchronous frameworks will run under
2:37 standard WSGI servers, but they will only run in their standard synchronous mode. They don't actually take advantage of the asynchronous capabilities,
2:46 even if they have them. So if you were to run some sort of framework under a WSGI server and test the scalability,
2:52 well, you're not actually doing any async and await, potentially. So what we're gonna do is we're going to make sure that we want to work with
2:59 an ASGI server and the one that we've seen so far, and the one that we're going to use for the rest of this course is
3:05 Uvicorn. And That's a pretty awesome logo. Come on. You can see it's a lightning fast ASGI server and it's
3:12 actually built upon uvloop and httptools. Uvloop is implemented in C++ so it's very low level,
3:20 very fast and so on. This has lots of good support for many of the things that you might want to do,
3:25 and it's a solid production server from the same guys that built Django rest framework,
3:30 the same guys that built API star and starlette itself which FastAPI is based upon. So here's Uvicorn, we're gonna be using that. This is one
3:39 recommended possibility. But down here, notice I've given you some resources. If we come over here to awesome ASGI and
3:47 this is just one of these awesome lists. It shows you all the servers, the frameworks, the apps and so on that fit into this space.
3:53 So, for example, under application frameworks, we have channels, Django, and WOOP WOOP FastAPI.
3:58 But there's also Quart, Responder, Sanic, starlette itself, which FastAPI is built upon, and so on. And coming down here we have some monitoring,
4:08 realtime stuff, these are not super interesting. Until we get down to here, so we have uvicorn, that's the one we talked about.
4:15 There's also Hypercorn and Daphne. All these are looking quite neat. Right now, this only supports HTTP/1 not HTTP/2. Hypercorn looks pretty good.
4:26 Haven't done anything with it, but maybe it's worth checking out actually looking at this right. So it'll show you,
4:31 here's how you test, like they talk about using httpx to test some of these things and so on. So if you're looking for stuff that fits into this realm,
4:38 you can check out awesome-asgi. So we're not really gonna run anything exactly here but I did want to talk about the difference between WSGI and ASGI
4:47 or WSGI and ASGI and that it's super important if you plan on writing code that looks like this, run FastAPI under an ASGI server,
4:57 might as well run it, it's built on Starlette, might as well run it on uvicorn,
5:01 which is built by the same people who build the starlette foundation of FastAPI anyway.


Talk Python's Mastodon Michael Kennedy's Mastodon