Python for .NET Developers Transcripts
Chapter: Deploying Python web apps
Lecture: Running in a production WSGI server

Login or purchase this course to watch this video and the rest of the course contents.
0:00 We saw this admonishment here and you should run this in a production WSGI server instead. Yes, yes we should.
0:07 We should also turn debug mode off, which we can also do. So we're going to do this and that brings us over to a new set of files.
0:17 We've got this Nginx, we got this service file. This one controls uWSGI and it's going to run as a system daemon in the background.
0:25 Think Windows service, auto start. It's sometimes hard to see the errors and where they went. They're going to be logged over to here, which is cool
0:35 but what we want to do, is we want to just test this command without the log bit first. So let's go. It's resisting scrolling.
0:48 Now also, it says we want to use this WSGI app. We haven't defined this yet so let me go ahead and put that in here.
0:56 Put a file called wsgi, you can call it whatever you want. This is kind of a bit of a convention. It will say from Guitary, not app. Import app.
1:06 Now, the way that the production servers work is they don't just run your code. So remember, the way we got it was that we ran
1:14 our code and it called this function, which called main. We can turn this to false. Right, it ran this 'cause we ran this file
1:24 so it's kind of the startup for that file. It doesn't work that way. It just defines these pieces and it imports app.
1:31 And then it uses that directly, okay. Actually, then I guess this doesn't matter down here, does it? If there's any additional setup, like
1:40 let's look over here real quick. Notice this one. In main we're setting up the global INET and we're creating the tables and we're loading the data
1:48 and we're calling run. What we would really need to do, and let me make a function over here, method. And I'll call this configure.
1:58 So, in this case what we call main I'm going to actually run main. But if they don't do main we need to do something else.
2:05 So we can go in and say, else is the production case. So you still got to do this configure bit. Put it like that. Here we go.
2:14 Okay, so just in case you run the other one you're going to run into problems running uWSGI without this. But this world, we're not doing extra stuff.
2:23 We haven't gotten there yet, so there's not really anything we have to do. This is good, but you would do something like possibly run
2:31 the configure here, or just let it get called the way it was. Let's go in add.
2:48 All right, here we go. Here's our WSGI. We should be able to just run this. So, it's really good to test though.
2:57 Test this directly, 'cause it's annoying to go and always check the log file if something doesn't work. It's much easier to run it, see if this works
3:04 and then diagnose the other issues. So, when you're doing this stuff I recommend small steps at a time, okay.
3:10 So, let's run this over here and see where we get. Over in uWSGI, tell it it's using this virtual environment.
3:17 It's running in master mode with four subprocesses. That's like the supervisor and then subprocess worker processes, each get two threads.
3:27 Let's then import 5,000. Okay, think we're good. Woohoo, it looks like it works. Let's see, WSGI app added. All right, it seems like it works.
3:40 Sometimes you'll see some errors come through but I don't see any this time. Must have done it right. Okay, let's try this again. Here it is.
3:49 Look at that. First time we got it working. So, as we make requests here we could do slash guitars. We can do a redirect. No, just a 200.
4:01 Could do tar slash all. That'd give us all of our guitars back. Here you can see the electric wood grain. So, it looks like it's working.
4:12 But we have uWSGI theoretically potentially, maybe working. That's not quite enough though. Remember, I just ran it, I ran this command.
4:20 I didn't set it up to start when the server turns on and stop when the server shuts down. So, that's the next thing that we need to do
4:27 is how are we going to set that up? But of course, now if I try again error. No connection. So, to accomplish that what we need to do is copy this file
4:36 and it looks like everything's good. This is not right, this'd be a Guitary. You never create this file from scratch.
4:44 You always get it from somewhere else. For you guys you can get it from this course. Okay, so what we want to do is we want to get this
4:52 and we want to copy it. Let me just go ahead and put the fixed description. Not that it really matters but have it working right.
5:00 So, to make this do its magic we have to copy that file, which lives here, into etcetera. Systemd, system Guitary service.
5:10 And then we can control it by saying, "Start Guitary" "Stop Guitary" and so on. So, it would git pull. Got our updated service and then we, whoops
5:23 did not copy enough, did I? Want to do this copy, fingers crossed. No, got the path wrong, didn't I? Didn't I, 'cause that is not the right name.
5:38 Here we go. Let's try again with this new one. Right, no output. That means it works. Hasn't quite been started yet, so we have to say, "Start"
5:49 and see what that does. Ooh, no errors, that's good. We can say, "Status". Alright, looks like it's running and that's cool.
5:58 We could run valances and see if it really is running. Oh yeah, there it is, it's running. And then we could do HTTP against it.
6:07 Yeah, look at that, it's running. However, if we restart the server, it's going to go away. That's not amazing.
6:13 So we have to say, "Enable" and this will tell it basically now it's an auto starting service. It's going to start, based on the various signals
6:24 that we put in the file. So, over here you can see after. It's just logged that target. After the system log is available.
6:32 Yeah, this is up and running. It's great and if it crashes it'll auto start itself and so on, so pretty excellent.
6:41 One other thing we can do is we can come over here. Go look at that. All right, there's this uWSGI log. That's cool.
6:50 We can even say tail -f for follow -n 20. This, if we go to the other. Then we go make that request again. Now you see, here's all the output.
7:02 Live, go in to log file. So, this is great when you're working on it you can fire this up, put it off to the side and then as you interact with
7:08 it exceptions will show up here. Requests will show up here. All that sort of good stuff. Our systemd unit file this dot service file let us set uWSGI
7:18 the worker process and the overseeing supervisor process for them to run and auto start when our system just boots up. That looks like progress to me.

Talk Python's Mastodon Michael Kennedy's Mastodon