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:
- 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.
- You really should be using Python 2.3 or greater
- 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.
- The version I am testing is 3.1.3
- 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.
- 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):
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:
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: 12%