Saturday
21
Feb 2004

Checking out mod_python

(12:57 pm) Tags: [Software]

Since I have a new itch to build yet another web framework (am I not every engineer on the face of the planet?), and I am getting tired of the compilation cycle in Java during development, I have been checking out Python. I looked at several of the pyhton web frameworks, and some are very complicated, while some of them just try to be servlets, mod_python was the only one that I found that doesn’t do a lot of framework stuff (this is good, since I want to do that) :)

After digging deeper into mod_python, including installing it on multiple machines (RedHat, Debian, and Mac OS X), I have a few things to report:

  1. I am talking about mod_python 3.x, which only runs on Apache 2. If you want to use Apache 1.x, try mod_python 2.x, but YMMV. If you are not using Apache, stop reading this now, since mod_python is designed and built to expose the Apache API.
  2. You really should be using Python 2.3 or greater
  3. mod_python is currently a bit testy on Mac OS X. I did have the examples running, but have now lost them. Mac OS X support is still a work in progress. I hope to report more success on this front in the future.
  4. The version I am testing is 3.1.3
  5. If you want to program the webserver down at the API level, mod_python is perfect. As I am going to try and do a completly free web framework, this fits right into my plan. Working at the API level allows you to change a lot of things about processing web requests, which is actually pretty enlightening. You can read more about the Apache API.
  6. mod_python includes support for sessions and cookies, as well as a newly released processor for ‘Python Server Pages’. I will write more about these in the future

Overall, first impression is that it is clean and light, and this happens to be exactly what I am looking for. No introduction can be complete without some sort of microbenchmark :) And considering that I want a fast web framework, I had better start the manic performance chants now ;)

So, we start with the Hello World (obligatory):

mod_python Hello World
from mod_python import apache
def handler(req):
    req.content_type = “text/plain”
    req.write(”Hello World”)
    return apache.OK

We drop this into a python file somewhere (test.py), and add the following to our apache configuration:

Apache configuration for mod_python Hello World
<Directory /usr/local/apache2/htdocs/pytest>
    AddHandler mod_python .py
    PythonHandler test
</Directory>

Now when I hit http://sarge/pytest/test.py, I see “Hello World”. Easy enough, but is it fast enough? Good question.
Running ApacheBench (ab), with the command ab -c 30 -n 10000 http://sarge/pytest/test.py, I get 1073 hits per second! I think that could be fast enough, but we should test something else to compare it. I chose static files, since Apache is pretty fast at that. I wrote a static file containing the text “Hello World”, and I get 2100 requests/sec. So I am giving up quite a bit of overhead. This does not deter me. Is it good enough? Absolutely. Let’s try PHP. Same test (echo Hello World), and we get 1061 requests per second. So, mod_python seems to have the same overhead as PHP (4.3.4).

Popularity: 11%

4 Responses to “Checking out mod_python”

  1. Anthony Says:

    if you use a psp the performance should be somewhat better as it has built in caching. req.write() is not cached.

    Anthony

  2. Bernard Kerckenaere Says:

    Huh,

    I wouldn’t try naming your test file test.py if I were you (python allready has a module named test.py, which makes for funky errors depending on the PATH sequence).

  3. Scott Sanders Says:

    That is very true! Thanks for the heads up on that. I guess that is why the example in the mod_python dist is named mptest.py.

  4. krishna Says:

    iam using python 2.4 version but i dont know what version of apache have to install and how to install?

Leave a Reply