Full Web Apps with FastAPI Transcripts
Chapter: Deploying FastAPI on Linux with gunicorn and nginx
Lecture: Server topology with Gunicorn

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now that we've got our virtual machine running on the Internet somewhere, in this case up on
0:05 Digital Ocean, what we're gonna do is talk quickly about what applications and server, services on
0:12 that server are going to be involved and how they fit together. So this little gray box represents Ubuntu, our server.
0:21 What we're gonna first install, or first interact with, when we make a request to the server at least really we'll probably start from the inside out.
0:27 But the first thing that someone coming to the server is gonna interact with is this
0:31 web server called Nginx. Now Nginx serves HTML and CSS and does the SSL and all those cool things, but it's not actually where our Python code runs.
0:42 We don't do anything to do with Python there. We just say you talk to all the web browsers,
0:48 all, to the applications, everything that's trying to get to the web infrastructure. This thing is where they believe they're talking to,
0:55 and it is what they're talking to. But it's not where, what is happening. That's not where the action is,
0:59 right? Where the action is, is gonna be in this thing called gunicorn You saw that we used uvicorn,
1:05 which is the asynchronous loop version of gunicorn to run our FastAPI, but gunicorn is more proper server that is going to do things like manage the
1:17 lifecycle of the apps running. So, for example, if one of the apps gets stuck and that process freezes
1:22 up, gunicorn has a way to run in supervisor mode. So it can say, actually that thing is stuck or it ran out of memory,
1:30 let's restart it so the server doesn't permanently go down, it's just gonna have a little
1:34 glitch for one user, and then it'll carry on. In order to do that, gunicorn is gonna spin up not one but many copies of our FastAPI
1:42 application over in uvicorn, which we've already worked with. And this is where our Python code that we write,
1:48 our FastAPI lives. So when you think of, where does my code run? what is my web app doing? It's gonna be this uvicorn process, and in fact,
1:57 not one but many. For example, over Talk Python Training, I believe we have eight of these in parallel on
2:04 one of our servers. So when a request comes in, it's gonna hit nginx, it's gonna do its SSL exchange and all those
2:10 things that the web browsers do with web servers, nginx is going to realize, oh, this request is actually coming to our FastAPI application.
2:19 Depending on how we've configured it, it's gonna send a request either over HTTP or Linux sockets directly. gunicorn says okay, well,
2:27 we've got this request for our application, and there's probably a bunch going in parallel.
2:32 Which one of these worker processes is not busy and can handle requests? Well, this one. Next time a request comes in,
2:39 maybe it's this one. Another request comes in, maybe those two are busy and it decides to pick this one.
2:44 So it's gonna fan out the requests between these worker process processes based on whether or not they're busy and all sorts of stuff.
2:51 So it's gonna try to even out the load across them, especially it'll know if they're busy and not overwhelm any one of them.
2:57 So this is what's going to be happening in our server and we're gonna go in reverse. We're gonna install uvicorn and our Python web app,
3:03 then we're gonna set up gunicorn to run it. And once we get that tested and working inside the app, inside the server on
3:10 Ubuntu, then we're gonna set up nginx and open it out to the Internet and make this whole process that you see here flow through.


Talk Python's Mastodon Michael Kennedy's Mastodon