Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: Deployment
Lecture: Running under uWSGI

Login or purchase this course to watch this video and the rest of the course contents.
0:00 We saw that we have our code on the server and that it runs, if you just run it straight with Python in the development server basically.
0:09 Now the next step is going to involve uWSGI. Remember, uWSGI is the layer that actually in production, properly runs our code
0:18 our Python code. So what we're going to need to do is we're going to work on this file called a service file or a unit file.
0:25 So if we go over here and look at it what it does is it basically has a title. This is uWSGI PyPI. It's going to run the uWSGI command
0:35 with the virtual directory in this run in four processes on port 500 and so on. It's going to run in that folder
0:43 and it'll restart if there's an error, things like that. So what we want to do is copy this file over and run it. the best thing to do however though
0:50 is not try that right away because if it doesn't work you're hunting around for log files and other weird stuff that you're trying
0:58 to figure out what's going on. So the best thing to do is just copy this right here. This is the command we're going to run.
1:04 It's going to run with that in the path. And it's going to import somebody called WSGI which we need to create that really quick.
1:12 So over here at the top we're going to create a file and it's going to say from pypi_org.app import app. We're also going to need main.
1:24 And here we'll just do our little main trick will run main. But only if we're happening to run it as the app. We're not going to run it that way.
1:32 What we really want to do is just export this. So let's commit this really quick. All right, perfect. Now over here we do an
1:46 we'll see you that it's not here yet but it would do a get. Should come down and here's our WSGI. So we're going to go to there
1:56 and import the app instance from there. And we're going to just mount that to / okay. So what we want to do is just run this command over there
2:04 and make sure that it works while we're in this directory. So we're in the right directory. Now we're just going to try the command
2:13 and either is going to work or we're going to get an error. Let's see. Oh, pretty good. It looks like it's working.
2:20 Here we can see our app. We put some little printouts. It's configured in the flask app and registry and blueprints and so on. So this is all good.
2:27 How do we see if it worked? Well, we have to go do a request against it and we can't do it here 'cause if we exit out it's going to stop it.
2:33 So let's create another terminal. Go in there. All right, let's use our little HTTP utility that we created. I'm going to go against localhost:5000.
2:44 Remember our command that we issued over here we're listening on port 5000 not 5006 like we were in our devops. So let's see what we get.
2:54 Oh it would be super If I could spell correctly. Here we looked like we lost in H on the way. Makes a request what do we get? Yes, all right.
3:01 You can see over here, the request came in. I went pretty quick for the first time and the response code is 200 right there. That's good.
3:11 And of course, we can see our content. Here's our little fake site. So it looks like running uWSGI outside of the system daemon
3:19 just directly with that configure we pass along is working. That's a super good sign and definitely something you want to do
3:25 before you try to actually run it as a system daemon.


Talk Python's Mastodon Michael Kennedy's Mastodon