Friday, January 22, 2010

The power of python within Tomcat for powerful webapps (jython2.5.1)

Many companies are looking to speedup their development process. Unfortunately they are restricted to use well established webserver framework like tomcat and, in this example, they fill limited to Java.
Jython is coming to the rescue. Since September 26th 2009, Jython2.5.1 has been released and can be use to create servlet that runs inside apache tomcat application server. Jython allows you to write python that is running on java VM (100%) and let you use lot of python pure libraries and let you use all java packages with a python synthax.
If you try it, you might have issues like:
  • How can I create a simple jython2.5.1 servlet? (not deprecated jython2.2.1 that doesn't allow you to use most pure python libs)
  • Why do I get ImportError when I use standard python package? How can I fix it?
  • Where should I put jython2.5.1.jar?
  • Where should I put my python code?
  • Where can I get a basic example that is working?
  • Where should I put my pure python libraries?
So I have decided to package a simple example (download: HelloWorld.war).
Here are the steps you should follow:
  1. get tomcat (I used 5.5.28)
  2. get jython2.5.1
  3. cp ~/jython2.5.1/Lib tomcat5.5.28/share/lib (required to used std python libs)
  4. cp ~/jython2.5.1/jython.jar tomcat5.5.28/share/
  5. download example: wget
  6. cp HelloWorld.war tomcat/webapps
  7. download java jdk
  8. export JAVA_HOME=sun-jdk-1.6.0_02
  9. tomcat5.5.28/bin/
  10. try it: http://localhost:8080/HelloWorld/
If you want the flexibility of python but you are stuck with java within tomcat, jython is becoming a real alternative since they've release jython2.5.1.

PS: a war file is a zipped file, you can unzip it in tomcat/webapps for testing so you don't need to rezip it and restart the server. When you are done, simply do a jar cvf HelloWorld.war * in the tomcat/webapps folder and ship that single file to the client tomcat server (make sure jython is installed). If you want to add pure python libraries, you can simply add them into your war file, it will work.

Here is the time comparison of the same service:
  • python: wsgi httpserver
  • jython: wsgi httpserver
  • tomcat: java servlet jython2.5.1
It is interesting to see that tomcat servlet provide better perf

No comments:

Post a Comment