I would like to run multiple independent Jython runtimes, with
different Python source paths and independent, non-interacting module
namespaces. Different runtimes will contain modules with the same
name but different content -- slightly different or completely
different. No settings, caches, data, or anything else that might
vary from one runtime to the next should be shared.
At first glance, it should be possible to achieve this by using a
different classloader to load the Jython classes for each runtime.
Since versions of the same class loaded by different classloaders are
not only independent but type incompatible to boot, a copy of the
Jython library loaded through one classloader should have no
interaction with a copy of the Jython library loaded through another
However, this ignores the possibility that the two runtimes might
interact with each other through JRE classes -- there is only one
version of each JRE class, loaded through a single classloader.
(Actually, I don't know if it is necessary in principle, but it is
necessary for other parts of my application to interact with my Jython
code.) So there could still be conflicts, especially since some
information in Jython gets cached in the current thread state. As far
as I can tell, though, the thread-local information in Jython is
contained in thread-local variables in instances of classes loaded
from jython.jar and should therefore not result in collisions. (I.e.,
it is not stored using a pthreads-style key-value mapping stored in a
JRE object somewhere.) In any case, interactions that result in
Jython class instances from one runtime being passed to the other
should result in ClassCastExceptions, not silent corruption.
With that preamble:
1. Does anyone happen to know a reason why it would be impossible to
create multiple independent runtimes using Jython?
2. Is anyone confident that it can be done?
3. Has anyone actually done it?
I have made a first try at it, but I've hit a problem. Specifically,
I have two runtimes that both contain a top-level module foo. Runtime
A contains the module foo.bar, but runtime B contains the module
foo.baz. Runtime B is initialized first and seems to run okay. Then,
when I attempt to instantiate the class foo.bar.Bar in runtime A, the
import of Bar succeeds, the Bar constructor runs successfully, but
when the runtime attempts to initialize the proxy, it cannot find bar
in foo. Debugging reveals that the foo module consulted during the
proxy initialization contains baz, but not bar. So some kind of
cross-talk is happening between the two runtimes, and no
ClassCastExceptions are thrown.
It might very well be due to a problem in my code, but I have worked
on it far enough that I figured I should ask for input before
proceeding further. Any information would be much appreciated!