Blog do projektu Open Source JavaHotel

wtorek, 8 stycznia 2013

Google App Engine and Jython

I'm planning to use Jython with Google App Engine for Java. My purpose is to use Jython as a scripting language for Java based solution, not to use Jython for server code development. I was interested to check if I can launch Jython (Python) packages at the server side. It was successful but with several drawbacks. The source code (Java and simple Jython package) is uploaded. The application (simple extension to default Eclipse plugin application) is available here.
Problem, strange behaviour in Development Mode
While running the application in Development Mode the following exception was thrown

Caused by: java.lang.NoClassDefFoundError: is a restricted class. Please see the Google  App Engine developer's guide for more details.
I found that the culprit is the following method in
 private void initEncoding() {
        String encoding = registry.getProperty(PYTHON_CONSOLE_ENCODING);
        if (encoding == null) {
        for (PyFile stdStream : new PyFile[] {(PyFile)this.stdin, (PyFile)this.stdout,
                                              (PyFile)this.stderr}) {
            if (stdStream.isatty()) {
                stdStream.encoding = encoding;
The is not on the Google App Engine "white list" but the same code is running after deploying to Google App Engine environment. The solution is to remove file.encoding property (although this property is set to not null value in Google App Engine).
Not blocking exception 
The following exception is also thrown
Caused by: access denied (java.lang.RuntimePermission modifyThreadGroup)
 at java.lang.SecurityManager.checkPermission(
 at java.lang.ThreadGroup.checkAccess(
 at java.lang.Thread.init(
 at java.lang.Thread.(
It seems to be related to the fact that threads are not allowed in Google App Engine. But this exceptions seems to be not blocking.
    static String getrVal() {
        putMessage("Moment 1");
        String encoding = System.getProperty("file.encoding");
        putMessage("Encoding:" + encoding);
        PythonInterpreter interp = new PythonInterpreter();
        URL ur = JythonMeth.class.getClassLoader().getResource("resource");
        String sRe = ur.getFile();

        interp.exec("import sys");
        interp.exec("print sys.path");
        interp.exec("sys.path.append('" + sRe + "')");

        interp.exec("import sys");
        interp.exec("print sys.path");

        putMessage("Moment 2");
        interp.exec("import mypack");
        putMessage("Moment 3");

        interp.exec("from mypack import myprint");
        putMessage("Moment 4");
        putMessage("Moment 5");
        interp.exec("GG = myprint.getVal()");
        PyObject sy = interp.get("GG");
        PyString u = (PyString) sy;
        String ss = u.getString();

        Map<object pyobject="pyobject"> m = new HashMap<object pyobject="pyobject">();
        PyObject keyS = new PyString("value1");
        m.put("key1", keyS);
        keyS = new PyString("value2");
        m.put("key2", keyS);
        keyS = new PyString("value3");
        m.put("key3", keyS);
        PyStringMap pyMap = new PyStringMap(m);
        interp.set("GG", pyMap);

        return ss;
The performance after deploying to Google App Engine is not satisfactory. But it depends on the machine type set as a host. This time table is related only when server is running the code for the first time, the second and the next are executed in no time. But in case of Google App Engine a request can be redirected to a fresh machine any time, so this problem really matters.
Machine Moment 1-2 (sec) Moment 3-4 (sec)
F1 11 6
F2 7 6
F4 5 3
Desktop 2 4
F1, F2 and F4 are described here. Moment 3-4 is related to the compiling of the junit package and can be improved by precompiling the jython package before deploying. Moment 1-2 is the time consumed by the initialization of the Jython. I do not understand why there is a such difference between local desktop and Google App Engine environment and do not see any solution to that problem.

Brak komentarzy:

Publikowanie komentarza