Python for .NET Developers Transcripts
Chapter: Deploying Python web apps
Lecture: Concept: uWSGI

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's review our uWSGI configuration and how we got uWSGI to run under systemd as a systemdaemon. So what we did is we created this thing
0:09 called a systemd unit file and it has this certain format that you can see various things you can set here and so on.
0:16 I've highlighted the really important parts. So we have ExecStart. This is the command that runs this daemon.
0:22 So what we're going to do is we're going to run uWSGI out of our virtual environment and we're going to tell it
0:27 your home is the virtual environment you come from, so. Also, go back and just /apps/venv. Run in master mode with four sub processes
0:35 four worker processes, that it's going to coordinate and each of those get two threads. It's going to listen on HTTP on port 5000
0:42 as opposed to, say, Linux sockets. Going to manage the script name. We're going to add to the Python path. We actually did that explicitly in our code.
0:51 Remember that that imports this in OS and we did that little line at the top. We could've technically avoided that
0:56 if we only ran it this way, rather than trying to run it directly in Python. I kind of like it. That it's super easy to run just in Python all over
1:03 but this also fulfills that role so technically, we could avoid it but it seems like not a bad idea to throw in, so I did.
1:10 Then we have to say, where is the Flask app that we're going to call run-on, located? We created a wsgi.py. File an imported app into it
1:19 so this mount here is saying, go to that file WSGI.py, the module and then, look for a thing called app and run that. Then finally, we just said, hey
1:28 login this directory, right? And that's the log output. You want to make sure that this process has permissions to write to that log.
1:35 One quick note, this whole ExecStart is just one line. It just happens that it won't fit on one line. It's really, really long. There's no scroll bar.
1:43 Guess I could put one in PowerPoint but it doesn't seem like a good idea so just be aware that it's just one huge and long line with wrapping.
1:49 And we also put the runtime directory for where our web app is located same as the Python path addition there. That's it.
1:57 And the way you work with these, basically, is you get one, you copy it over, you tweak it make minor tweaks to it, so it'll run and then, you're good.
2:04 Now, also remember, when you're doing this infrastructure stuff, little, tiny baby steps. Don't do it all and then, try to test it.
2:10 So for example, I didn't just try to run this. I copied the ExecStart without the log parts so I could see what was happening.
2:17 Then, I tried to run that to see if that was going to work. If something goes wrong it's much easier to see what's happening
2:23 by trying to run that directly rather than, well, we're running systemd then we're enabling the service, and it's starting.
2:29 Then, it's logging over there, but maybe it won't log there if it doesn't have permissions to log there. And there's just like a bunch of other layers
2:35 you want to verify, so baby steps. Try each thing along the way. Finally, when we were convinced it was going to work
2:41 we copied this file over to etcetera, systemd system guitar a.service. Them we issue the commands the systemd to say
2:49 enable the service that created this SymLink so that it'll autostart when we reboot the system. That was it. We had our Python code running the server.


Talk Python's Mastodon Michael Kennedy's Mastodon