Eve: Building RESTful APIs with MongoDB and Flask Transcripts
Chapter: Fine-tuning your REST service
Lecture: Deploying your service
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
So far we've been using the Flask builtin server for running our apps.
0:04
Now while it is lightweight and easy to use,
0:08
Flask's builtin server is not suitable for production
0:11
as it doesn't scale well and by default it only serves one request at a time.
0:18
Some options available for properly running Flask in production
0:21
are documented on the Flask website.
0:24
On the website you'll find instructions
0:26
on how to host your app on all kinds of services
0:30
or you can find instructions on how to set up Flask
0:34
on your own self hosting environment.
0:37
In this lecture, we'll learn how to run our app on Gunicorn.
0:42
Gunicorn stands for green unicorn,
0:44
it's a wsgi http server for unix.
0:48
Actually, a pre fork worker model ported from Ruby's unicorn project.
0:52
The good news is that running a Flask app on this service is very simple
0:56
Gunicorn comes as a standard Python package
1:00
so all we have to do is pip install it in our environment.
1:08
Now that it is installed, all we have to do is launch it,
1:13
pass the name of the launch script
1:18
and within the launch script what is the object holding the wsgi app.
1:23
So in our case the app.py script contains an apt object
1:31
and boom, we're done, the server is running on local host port 8000.
1:37
Now Gunicorn comes with a number
1:40
of interesting and powerful command line options
1:43
the most useful probably are
1:47
-w which allows us to specify the number of workers.
1:53
And that should be where we can say what is the port we want to listen to.
2:05
So in this example, I'm launching our app with 4 workers
2:11
and the listening on port 4000.
2:15
Right. As you can see, we have 4 workers,
2:19
1, 2, 3, and 4 all listening to local host port 4000.
2:26
Let's go to Postman and try to send a request.
2:30
to our port endpoint people— it works.
2:38
That's it, of course, you have many more options.
2:41
Gunicorn is just one of the many options you have,
2:45
but surely it could be a nice starting point,
2:48
and it also offers very good performances.