Primary image for Serving Django via CherryPy

Serving Django via CherryPy

Download django-cpserver Now at GitHub

Background

A few months ago, I got sick of trying to deploy Django sites on my cPanel server and got a VPS at Slicehost. Thanks to SuperJared, setting up Apache/mod_python behind an Nginx frontend was a snap.

I started deploying and migrating sites to the new server and kept an eye on my server resources via Munin. I had about 10 sites running on a 1GB Slice, but the Apache processes were hogging all the RAM. Restarting Apache would bring memory usage down to around 500MB, but within a couple of hours, it would be using all my available RAM, with individual proceesses using as much as 120MB.

I started asking questions and trying different options including mod_wsgi, verifying projects weren’t in debug mode, etc. Nothing made a difference.

CherryPy to the Rescue

I came across Loic d’Anterroches’ script to run Django via CherryPy and figured I’d give it a shot. Here are the results:

Memory Usage
those drops on the left side are Apache restarts
Memory usage went from 800-900MB to ~300MB. Whoa! Each server was using 20-50MB. I ran some ab tests against it and found it actually held up quite well against Apache/mod_wsgi for low to medium traffic. As expected Apache scaled better to higher traffic. For my needs, CherryPy was more than enough.

Implementation

I reworked the original script to be more Django-like and run as a custom Django management command. In its simplest form, the server can be started via:

$ ./manage.py runcpserver
Advanced usage details including running on a different port, specifying <span class="caps">SSL</span> certs and spawning as a background process run by a different user are available by calling <tt>./manage.py runcpserver help</tt>.

Installation

  1. Download django_cpserver, unzip and add it to your PYTHONPATH.
  2. Add django_cpserver to your INSTALLED_APPS.

Additional Resources

Peter Baumgartner

About the author

Peter Baumgartner

Peter is the founder of Lincoln Loop, having built it up from a small freelance operation in 2007 to what it is today. He is constantly learning and is well-versed in many technical disciplines including devops, …