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