I'm working on a little webapp project. Usually I just use plain old Python CGI scripts for these things but this time I actually care about performance a bit so I did some delving into the modern world of Python webapps. Things are a lot better than they used to be. WSGI is the standard Servlet API, web.py (small) and Django (big) are good app frameworks, and Zope is mercifully sliding to retirement. But I'm stubborn and want to do things my own way, just use FastCGI to avoid invocation overhead.

Turns out it's easy on a Debian system. Install the packages python-flup and libapache2-mod-fastcgi. Then write a python2.4 program like this:

#!/usr/bin/python2.4

import flup.server.fcgi
g = 0

def app(environ, start_response):
  global g
  status = '200 OK'
  response_headers = [('Content-type','text/plain')]
  start_response(status, response_headers)
  g += 1
  return ['Hello world! %d\n' % g]

if __name__ == '__main__':
  from flup.server.fcgi import WSGIServer
  WSGIServer(app).run()

The one badness is that modifying your Python code won't force a code reload, which sucks for development. If you name your program "foo.cgi" Apache will invoke it as normal CGI and Flup supports that too, so that's what I'll do for now. Even if it behaves differently.

tech
  2006-08-15 18:44 Z