Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Replace Django's Development Web Server With Gunicorn

For more information on concepts covered in this lesson, you can check out:

Here are the command-line snippets used in this lesson:

$ tree portfolio/
$ vim portfolio/
$ source venv/bin/activate
$ python -m pip install gunicorn
$ python -m pip freeze > requirements.txt
$ vim Procfile
$ git status
$ git commit -am "Replace Django development web server with Gunicorn"
$ git push heroku
$ heroku open
$ heroku logs --tail
$ heroku config:set WEB_CONCURRENCY=4


web: gunicorn portfolio.wsgi

00:00 In this lesson, you’re going to replace the development web server built into Django with a proper HTTP server that can handle traffic concurrently and more securely.

00:10 One of the most popular choices for Django is a pure Python implementation called Gunicorn. When you started a new project, Django generated a management app along with a special module called wsgi, which stands for the Web Server Gateway Interface.

00:26 WSGI is a Python specification that unifies the interface across different web frameworks, making it easier to interoperate with web servers and replace one framework with another.

00:37 The file generated by Django contains glue code that lets WSGI-compliant web servers such as Gunicorn communicate with the Django framework. You don’t need to know about the file’s contents, but if you’re curious, then there’s actually very little code in there.

00:52 Every time your web server receives a request, it will delegate it to a callable object named application, which conforms to the specification.

01:04 To install Gunicorn, make sure you’ve activated the right virtual environment and use pip.

01:15 Remember to freeze your dependencies and refresh the requirements file afterward. Next, modify the Procfile to take advantage of Gunicorn.

01:30 You’ll need to invoke the Gunicorn command followed by a fully qualified name of the WSGI module in your portfolio management app that you just saw. Notice that you don’t need to provide the network interface nor the port number.

01:43 Gunicorn can pick those from the environment variables set by Heroku.

01:53 Finally, commit and push your changes to Heroku. They should include the updated requirements file and the Procfile.

02:09 When the build is finished, check if you can still access your Django project on Heroku. Okay, perfect. Now, open the Heroku logs and look for the gunicorn command.

02:27 You’ll notice that it runs two workers that can handle HTTP requests independently, increasing the overall throughput of your app. The number of worker processes is configurable through an optional WEB_CONCURRENCY environment variable. Try increasing the number of workers as an exercise, redeploy your app, and verify the logs. In the next lesson, you’ll learn how to connect your app to a relational database in the Heroku cloud.

Become a Member to join the conversation.