Python for .NET Developers Transcripts
Chapter: Deploying Python web apps
Lecture: Concept: Web server architecture
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Before we start configuring and installing our server let's just take a step back and look at all the moving parts. So what we're going to do
0:07
as we said is we're going to set up an Ubuntu machine. That's the Ubuntu logo in the corner by the way.
0:13
We're going to set this up and manage it ourselves. We talked about all of the different options and there's many things you can choose.
0:18
For this course I'm going to show you this because like I said, I think gives you the most flexibility. So what software's involved and
0:24
how does it fit together? Well, there's actually two web servers so we're going to install this thing called, Nginx. It's a free open-source web server
0:32
and it is really really awesome. However, it doesn't run Python Code. It just handles web traffics, static files SSL, that kind of stuff.
0:43
For what we want to do we also need to run our Python code. Obviously, that's the logic and implementation of our web app.
0:49
So, we're goin to use another server called uWSGI. The U is like a MIU, a little greek, Miu so uWSGI. Remember Web Service Gateway Interface.
0:59
This thing can host any WSGI Python application. So Flask, Django, Pyramid, bunch of others. We're going to do a couple of things
1:07
to improve the scalability of our web app. One, we could have a bigger server with more cores. We could actually have multiple servers
1:14
with load balancers. All sorts of stuff. But even on this one server here instead of just running one uWSGI we're going to run uWSGI
1:22
whose job is to communicate with a bunch of little uWSGIs. All these little guys. This is actually where our Python is going to run.
1:30
We have all of these little worker processes. They'll basically be running copies of our app. Remember Python's GIL and how that can be a problem?
1:37
And how multi-processing solved it? Well, here we go. We have a whole bunch of parallels have added by doing this amongst other things.
1:45
Like, if one gets stuck locked up, runs out of memory we can use all the others. So our requests are going to come into this world
1:52
over ACDPS, it's going to talk to Nginx. Nginx is what the world believes our web server is. They talk to Nginx.
1:59
They never see or know about uWSGI at all. They talk to Nginx. That's that. This is going to handle the SSL
2:07
maybe even serve them back the static files. It won't go to the Python Code for that. If there's a data driven request
2:12
it's going to go into our Python application it gets delegated over here maybe over HTTP or maybe even Linux Sockets. It's going to figure out
2:20
Well, which one of these is free? Well, that one's free. Let's let that one handle the request. Another request comes in, it says
2:25
Oh. That one's free. Another request comes in, it says Oh, this one! This is the one that's free! and they're going to be running
2:29
potentially in parallel, processing the request. So, this is the overall pictures. What we have to do to make this work is we have to setup Nginx
2:37
with the right configuration we have to setup uWSGI. We have to get our code on the server and setup the server with the right dependencies.
2:46
We're actually going to do that in reverse order. We're going to put the dependencies then put our code, then uWSGI. Make sure that uWSGI working
2:52
on local-host and then we're going to set up Nginx to talk to the world and let Nginx actually close that gap there
2:59
and talk to uWSGI on local loop-back or over our socket. Before you're done that folks this is not that different than IS.
3:05
IS handles all the static responses and so on. We have the ASP.NET worker processes that run. You can even set them up in this kind of
3:14
Farm Mode that we're talking about here. Though we have ISS and ASP.NET worker processes Nginx is like the ISS.
3:22
uWSGI is like the ASP.NET worker processes that would run your .NET C# code but of course, we're running Python code in this Python world.