Question

Is it possible to run more then one Jython environment with different system settings (i.e. library import paths) under one JVM.

If it possible please suggest how this can be done properly.

Technically nothing prevents me from having PythonInterpreter interpreter = new PythonInterpreter(); executed more then once. But I would like to be sure that there is no singleton sitting somewhere inside.

Was it helpful?

Solution

After checking the source code it seems that PySystemState is singleton

Source code

public class PySystemState extends PyObject implements ClassDictInit {
...
    private static boolean initialized = false;
...

Source code

public static synchronized PySystemState doInitialize(Properties preProperties,
                                                 Properties postProperties,
                                                 String[] argv,
                                                 ClassLoader classLoader,
                                                 ExtensiblePyObjectAdapter adapter) {
        if (initialized) {
            return Py.defaultSystemState;
        }
        initialized = true;
...

This implies that PySysStatus properties are set only once and next call to get PySysState will return you the same Jython environment.

There is still way to use custom ClassLoader to initialize different PySysState in different contexts, but for my task at hand it is not required.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top