Chaussette and Circus as a new way to deploy your Django applications

How to use WSGI server Chaussette and proces, socket manager Circus to serve your Django application on production?

Many Python web applications run on production with the help of gunicorn or uwsgi. There are however other solutions that may turn out to be even better than the currently popular solutions. One of alternatives is chaussette and circus. Chaussette is a WSGI server that can serve WSGI applications like your Django project. Circus is an application to control and monitor processes and sockets. It can manage chaussette, celery or other sockets and processes making it a manager of all project services.

Chaussette

You can install chaussette with pip and run your Django application with:

chaussette my_application.wsgi.application

In case of Django the wsgi.py file is used. If you have custom settings files be sure to set the correct one in that file or chaussette may have problems starting your application.

Chaussette on its own won't give much. It need a supervisor or circus to manage it.

Circus

Circus is a process/socket manager and monitor. It's similar to supervisor but has bit different approach. We can install it via pip and after that we can start configuring it and services it should manage.

Circus differs from supervisor as it can work as a hub of all of project services. Local circus may manage all local services and servers of the project and be managed by system global supervisor. In a supervisor + gunicorn configuration the gunicorn was making a breach as it and other services where managed by the global supervisor. If you host many applications on one server circus + chaussette may help a lot.

Quick example - Django configuration

Now we can put the chaussette startup code into circus.ini so that circus can manage it. Let us start with such circus.ini:

[circus]
endpoint = tcp://127.0.0.1:5555
pubsub_endpoint = tcp://127.0.0.1:5556
stats_endpoint = tcp://127.0.0.1:5557

[watcher:web]
cmd = /path/to/application_virtualenv/bin/chaussette --fd $(circus.sockets.web) my_application.wsgi:application
use_sockets = True
numprocesses = 2

[socket:web]
host = 0.0.0.0
port = 8000

The start of the file is circus configuration take from one of the examples, next we have chaussette configuration in the "web" blocks (web is the application name within circus, it may be different). Parameters available for every block are in the documentation.

When we have a finished configuration file we can start circus with:

circusd circus.conf

If it works then we can use is it with nginx using such configuration:

server {
  listen  80;

  location /static/ {
      alias /path/to/static/;
  }
  location /media/ {
      alias /path/to/media/;
  }

  location / {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      proxy_pass http://127.0.0.1:8000;
  }
}

Where most important part is the location /. proxy_pass must be set to the IP/port on which chaussette is listening (configured in socket:web) - in this case we have 8000. Alternative way would be to configure chaussette to use sockets which then would have to be passed to proxy_pass.

Addons

circusctl is an interactive shell for circus. You can check or restart services managed by circus. After deploying a new version of your app you can even reload and restart everything:

circusctl reloadconfig --waiting
circusctl reload --waiting

For monitoring load you can use circus-top, or circus-web.

I hope this quick article will make you interested in chaussette and circus combination for your production configuration.

blog comments powered by Disqus

Categories