Blog

Serving Django via CherryPy

Posted March 25, 2008. Filed under code, django

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 SSL certs and spawning as a background process run by a different user are available by calling ./manage.py runcpserver help.

Installation

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

Additional Resources

Need to get everyone on the same page? Try Ginger! Hold productive discussions with your teammates across the globe or across the hall. Build consensus, hash out complex ideas, and eliminate meetings altogether.

Looking for help building your web app? Lincoln Loop is a full service web studio focusing on Django development. See what we can do.

Comments are closed for this post.