Wednesday, March 19, 2008

django+jython+jetty

UPDATE: since this is now 2 years old, and one of my key links is broken i've written a quick update here: http://tristanking.blogspot.com/2010/01/re-jettydjangojython.html

Yep, you read that right. Django, running ontop of jython, running inside jetty.

Why? The idea just popped into my head while in a meeting discussing our project's deployment strategy. I thought "hey, we already have a tomcat server running for other people's projects, I wonder if i can get django+jython running inside a servlet container". Seeing that jython already came with a PyServlet class, i figured the base work was already there and decided to give it a go.

So I started off looking at the PyServlet class that ships with Jython. PyServlet allows you to write servlets from python code, implementing the standard doPost, doPost, etc functions, from which you access by going to a URL which matches the path name of you .py file. This doesn't match the REST method django uses, so the PyServlet needed some work. It turned out to almost be an entire rewrite, the only things remaining from PyServlet being the PySystemState initialisation.

As well as the DjangoServlet, a django handler module had to be created to bridge the gap between the java servlet request and response objects and the django python request and response objects. The already existing modpython handler provided a great guideline to make this, and it turned out to be a breeze.

Currently, I build the DjangoServlet on it's own using maven and install it into my local maven repo (for people who have not used maven before, this involves a single command line call). The servlet handler module is put straight into django in the same location as the other handlers (django.core.handlers.servlet). I then have another maven project with a dependency on the DjangoServlet project, and a web.xml. The web.xml requires a few init-params: one for the jython home, one for the django handler class and one for the django app location.

With everything set up, using the maven jetty plugins, i fire jetty up, which loads the DjangoServlet and BAM! django running in a servlet container.

So far I've not noticed any difference in performance, but one nice 'feature' is being able to use maven's dependency management to set up the classpath with the jars required to run your app (it definitely beats having the edit the $CLASSPATH before starting jython and my settings.LIB_DIRECTORY method).

A few things still need to be done to make this really easy to setup and use, It may be best to try move the handler module out of the core django code, so that once jython is up to the task of running django without any modifications it is possible to deploy this on a pure django+jython install. I also need to write scripts to build the pom.xml and web.xml files.

If you want to have a look at this, or deploy it yourself, you can grab the projects from github. Here for the DjangoServlet project, servlet.py for the django handler module and hydrant is my django app which has example pom.xml and web.xmls for you to use as a reference for your own (until i have proper scripts/docs to build them).

8 comments:

Henrik said...

Really cool stuff. Good job. I had plans on doing something similar a while back when I was playing around with CherryPy + Jetty + Jython. In that work I used modjy. Have you thought about using modjy and the django WSGI handler? I have no idea if it would work...

diecast said...

what a hassle, just to integrate with customer's java environments. but hey, well done. ;)

Anonymous said...

Do you want to know the magic of online games, and here you can get more angels gold. Do you want to have a try? Come on and angels online gold can make you happy. You can change a lot buy angels gold for play games. And you can use the cheap angels online gold do what you want to do in the online game.
Do you want to know the magic of online games, and here you can get more Rose zuly. Do you want to have a try? Come on and rose zulie can make you happy. You can change a lot rose online zuly for play games. Playing online games can make much rose online zulie. And you can Arua ROSE zuly do what you want to do in the online game.

Anonymous said...

Have you heared about a game which you need use priston tale Gold to play, and you can also borrow priston tale Money from other players? But you can buy priston tale Gold, or you will lose the choice if you do not have cheap priston tale Gold. If you get it, you can continue this game.
Have you heared about 9Dragons which you need use 9Dragons gold to play, and you can also borrow 9 Dragons gold from other players? But you can buy 9 Dragons gold, or you will lose the choice if you do not have cheap 9Dragons gold. If you get 9Dragons money, you can continue this game.

Anonymous said...

I like holic gold very much. Since I entered into this game, I learnt skills to earn holic money. Thanks to holic online gold let me know a lot of friends. It is my habit to buy holic online money, and I get some cheap holic gold from my friends and Internet.
I like to play last chaos, because I like its name, also I like last chaos gold. My friend told me that she would buy lastchaos gold for me, and I was so happy. I do not like to go shopping, because it always spends a lot of money, but I never hesitate to buy last chaos gold. You can buy cheap lastchaos gold; it is so easy and convenient. I believe the future of lastchaos money is bright.

Emblem Parade said...

Hey, the servlet.py doesn't seem to exist anymore. Any chance you can get it back online or email it to me? I'm doing some cool experiments with Django/Jython!

Unknown said...

Excellent task ! Your internet page has supplied me much of the tips I desired .
Naperville locksmiths
Locksmith MARGATE FL
Berkeley locksmith
Locksmith Mountain View CA
Locksmith Mountain View
Mountain View locksmith
Locksmith Mountain View CA
Locksmith Mountain View CA
Elgin Locksmith
Locksmith Elgin IL
Locksmith Elgin
Elgin Locksmith
Locksmith Elgin
Locksmith Elgin
Elgin Locksmith
Locksmith Elgin
Elgin Locksmith
Locksmith Elgin IL
Locksmith Elgin IL
Locksmith Elgin
Locksmith Elgin IL
Elgin Locksmith
irvine locksmith
irvine locksmith
pembroke pines locksmith
Aventura FL locksmith
locksmith hialeah
locksmith hialeah
irvine locksmith
locksmith hialeah
pembroke pines locksmith

dolinna said...

This is the good news.The information you provide is very useful for me.Thank you for sharing it.I will come to see if you have new post every day.I am a china tour lover,You can learn more: China vacation packages | China Travel Agency | Tibet Tours