jython question (interesting behavior)

P

pythonUser_07

Is this the correct place to post a jython question?

I posted the following in the jython group, but I figured I'd post here
too:
_________________________________________
I am assuming that the PythonInterpreter environment is not a unique
environment from within a jvm.

Here is some pseudo code to show what I am talking about
1) create a jython module file, lets call it "mytest"
with a simple static class
class simpleS:
myVar = "test01"
2) Now set up two interpreter environments from within
the same jvm
PythonInterpreter py = new PythonInterpreter()
PythonInterpreter py1 = new PythonInterpreter()
3) now in both interpreters, import the module
py.exec("import mytest")
py1.exec("import mytest")
Now for both interpreters run
"print mytest.simpleS.myVar"
4) Now for the crazy part.
in the py interpreter run
"mytest.simpleS.myVar = 'test02'
in py1 look at the value
"print mytest.simpleS.myVar"

Very interesting behavior. So it seems that each python interpreter
instance does not act as its own seperate space.

Maybe I just have not gotten there yet with the docs. I did notice
something about the initialize() function associated with the
interpreter which is only supposed to be run once.
 
P

pythonUser_07

Hmm, now that I think about this, maybe it's not so crazy.

It would be the equivalent of modifying a static variable in the same
JVM. Sorry to bother.
 
K

Kent Johnson

pythonUser_07 said:
Is this the correct place to post a jython question?

I posted the following in the jython group, but I figured I'd post here
too:
_________________________________________
I am assuming that the PythonInterpreter environment is not a unique
environment from within a jvm.

Here is some pseudo code to show what I am talking about
1) create a jython module file, lets call it "mytest"
with a simple static class
class simpleS:
myVar = "test01"
2) Now set up two interpreter environments from within
the same jvm
PythonInterpreter py = new PythonInterpreter()
PythonInterpreter py1 = new PythonInterpreter()
3) now in both interpreters, import the module
py.exec("import mytest")
py1.exec("import mytest")
Now for both interpreters run
"print mytest.simpleS.myVar"
4) Now for the crazy part.
in the py interpreter run
"mytest.simpleS.myVar = 'test02'
in py1 look at the value
"print mytest.simpleS.myVar"

Very interesting behavior. So it seems that each python interpreter
instance does not act as its own seperate space.

My understanding is that the two PythonInterpreters wrap a single PySystemState which is where the
modules are cached. So they share the same modules. If you want different behaviour you should give
them each their own PySystemState.

Kent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top