Jython Journeys

Notes about my work with jython and python

Jython on Google AppEngine: Why bother?

without comments

Why on earth would you want to run jython applications on Google Appengine?

First off, I must apologise for the flippant nature of the attention grabbing title. I’m a jython committer, and a jython user, so I believe that there are very good reasons for running jython on Google AppEngine. (You can read how to do so on the modjy wiki: how to run jython applications on Google AppEngine).

The title is essentially a paraphrase of a statement made by a Google AppEngine Evangelist, at a talk about AppEngine that I attended here in Dublin. The speaker was of the opinion that there was no valid reason for running jython on AppEngine, a statement which I couldn’t let pass.

I think that the core of his reasoning for making that statement was a simple misunderstanding, but a misunderstanding that is common among users who are familiar with python, or users of cpython, but who are not familiar with jython’s use as a JVM scripting language. The misunderstanding is that the only reason for running jython is so that one can run python applications on the JVM, using python’s wealth of "batteries included" APIs. If that is the case, then why bother running jython, when one can simply use the cpython support on AppEngine directly?

To explain, when writing a cpython application that processes email, for example, one can make use of the python’s email module, which provides a wealth of features for dealing with the many complexities of parsing, processing and generating email messages, MIME, etc. Users of jython can also use the python email modules, since those modules are written in pure python. But since jython runs on the Java Virtual Machine(JVM) and permits seamless integration of java and python code, jython users writing email processing also have the option of using the excellent JavaMail APIs, which are just as robust as the pure python modules, and arguably more efficient. So jython users have the best of both worlds: they can use either python APIs or java APIs, with equal ease.

Processing email is perhaps too straightforward an example, in that both python and java have excellent email processing support. But due to the more widespread use of java in the development community in general, java generally has far more support for any given technology need, since there are often multiple options available. A good example of this is cryptography: there is a wealth of java cryptography libraries available, including the excellent Bouncy Castle, Cryptix, Flexiprovider, etc. The choice of crypto libraries in python is generally narrower, supports less functionality, and is sometimes abandon-ware.

So if one’s understanding of jython is simply that it is a way to run pure python modules on the JVM, then the above mentioned mis-understanding is forgivable. But it is simply not the case that the primary usage of jython is to run pure python modules: Jython excels as a dynamic/scripting language for the JVM, and permits the use of java APIs, without having to write java code.

In discussion with the Google engineer after the talk, he conceded that there was one possible use case for running jython on AppEngine, namely that of prototyping java applications. In this scenario, applications would

  1. Be developed in jython, taking advantage of the enormous productivity increases gained by developing in python
  2. Be translated to java once the functionality of the application is complete

My reply to this is: “Why bother with step 2”?

If you’ve got a running application that is fully functional and written in jython, then you don’t need to translate it to java. You should only consider translating it, or parts of it, into java if you really have to. For example, after profiling your jython application, you may find one or two core pieces that are performance bottlenecks: these are prime candidates for translation to java, to improve performance. But there’s no reason to translate the rest of the application: maintainability and productivity is best served by simply leaving the jython code exactly as is.

I suppose a part of the reason why this mis-perception of jython has come about is because python is such an excellent language that it is in the unique position of having two completely independent implementations of the language that run on Google AppEngine. But just because both cpython and jython run on AppEngine doesn’t mean that their usages should be identical. Jython’s place in the AppEngine ecosystem is primarily as a JVM dynamic language.

So, in summary, jython is more than just a mechanism for running pure python code on the JVM: it is an excellent JVM development platform in it’s own right.

I hope I’ve given you some good reasons to consider developing your next Google AppEngine project on jython.

Written by alan.kennedy

November 30th, 2009 at 2:40 pm