Full Web Apps with FastAPI Transcripts
Chapter: Creating our first FastAPI *site*
Lecture: Ready for running in production

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now, before we call our little basic structure here
0:03 finished. I do want to show you one quick little thing.
0:06 Notice I was able to go over here,
0:08 right click and say run.
0:09 And I can also press this button and everything looks golden.
0:12 But the way that we actually run FastAPI in production is not to
0:18 go and say Python run this file.
0:20 What we're gonna do is we're gonna say set up Gunicorn,
0:23 which is going to oversee a farm of Uvicorn worker processes,
0:27 and each one of those is going to run this application.
0:31 And that way, if something goes wrong with one,
0:33 it can be restarted. You can do scale out.
0:35 There's all sorts of cool advantages to that.
0:37 And that's generally how Python web apps run in production anyway.
0:41 And when we do it in that mode,
0:44 the way we're gonna have basically implicitly behind the scenes do that chain. I talked about
0:48 what's gonna happen is, we're going to say uvicorn main thing here.
0:52 What is it that we're working with?
0:53 And then what is the name of this thing here,
0:57 which is app in our example. So we're gonna go to the main module and find
1:01 the app instance and run it.
1:03 Watch what happens if I try to run it here. It says
1:05 whoa, whoa. You're somehow like double running it.
1:08 I'm not really sure what's going on here.
1:10 And that's because this is really just for testing,
1:13 and we need a different way to run it in production.
1:15 So we're just gonna quickly fix that and say:only
1:18 if you try to run it here in dev,
1:21 do we want to do that.
1:22 So notice this, a live template is gonna expand out to if the name is main.
1:26 This is the Python convention. Say this file is being run directly rather than imported
1:31 and else I don't know.
1:36 This is what we're gonna end up doing in production.
1:38 There's actually some things will ultimately need to do here.
1:40 But for now, there's just nothing else happening.
1:43 If I run over here through this mode,
1:46 you can see it's running fine.
1:48 And if I go into the terminal now and I try to run it like this
1:53 hey, look, it runs fine as well.
1:55 Okay, so you'll see that as we get this little bit more complicated.
1:59 Think more things going on, like setting up the database at start up and so
2:02 on. There's gonna be some interesting balancing act that we have to do around this
2:06 but I wanted to make sure that you I focused on this specifically because it's
2:10 easy to just say uvicorn run,
2:12 but then seems like it works and then it doesn't work in production.
2:15 And that's why, right, this is fine when we press the run button
2:18 but in production were running through a completely different chain of events that uses
2:23 uvicorn module:variable_name.
2:28 So I want to make sure we get this set up right from the start.