Tuesday, December 2, 2008

Cherrypy + Routes with mod_wsgi

If you haven't played around with deploying your python apps using mod_wsgi, I highly recommend it. It is a very stable way to deploy your application that takes advantage of the good parts of the apache2 runtime.

Vanilla Cherrypy apps run great as wsgi applications. However, I did come across a problem when deploying a Cherrypy app with Routes. Running the application using the cherrypy server worked fine, but deploying it using apache2 + mod_wsgi always gave me the following on every request:

503 Service Unavailable

The CherryPy engine has stopped.

After some looking around the mod_wsgi project (which is well documented I must say) I found the answer here: http://code.google.com/p/modwsgi/wiki/IntegrationWithCherryPy

When running a Cherrypy app with the routes dispatcher you need to make sure you call:

cherrypy.engine.start(blocking=False)

After mounting your app. This sets the cherrypy engine state to running. After doing this everything worked great and I now have beautiful restful routes in my mod_wsgi app.

1 comment:

Anonymous said...

It isn't just when using Routes with CherryPy. You always need to startup the CherryPy internal dispatcher. You also need to ensure you register the atexit function for stopping it, else in some cases it may cause problems with Apache or mod_wsgi daemon processes shutting down.

I believe the need to start the internal dispatch engine may be going away for newer version of CherryPy, but not sure if that is out yet and which version.