Per engine share variable

I just moved from jruby 1.3 to jruby 1.5. I noticed that shared global
variables behave differently now.

In 1.3 I as able to do

engine1.put(“VAR1”, “val1”);
engine2.put(“VAR1”, “newval1”);

When I do an eval on the respective engine (on their respective rb
scripts), each gets its own value for $VAR1. Inside of the ruby script
I’m accessing them as global variables.

In 1.5, both gets the value of VAR1 that is set last. So both get
newval1.

Is the 1.3 behavior an incorrect behavior? Is there any way to get that
behavior in 1.5?

Thanks
Budyanto

I believe this depends on how you use the scripting engine. If engine1
and engine2 are configured to be isolated instances of JRuby, the
behavior you see is expected. The original behavior was, I believe,
closer to SINGLETON mode, where a single runtime was used to service
all requests.

Yoko, perhaps you can confirm this?

On Fri, Mar 5, 2010 at 5:21 PM, Budyanto H. [email protected]
wrote:

I’m accessing them as global variables.
Posted via http://www.ruby-forum.com/.


To unsubscribe from this list, please visit:

  http://xircles.codehaus.org/manage_email


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

By Isolated instance of JRuby, do you mean separate JVMs? Or is there a
way to run multiple instances of JRuby within the same JVM by setting
system properties?

Thanks

On Sun, Mar 7, 2010 at 8:07 AM, Charles Oliver N.
[email protected] wrote:

I believe this depends on how you use the scripting engine. If engine1
and engine2 are configured to be isolated instances of JRuby, the
behavior you see is expected. The original behavior was, I believe,
closer to SINGLETON mode, where a single runtime was used to service
all requests.

Yoko, perhaps you can confirm this?

Sure.

I found current JSR223 implementation of getScriptEninge() was weird.
Users can get a new engine instance every time
ScriptEngineFactory#getScriptEgine() method is called, but in the
lower layer, only one instance of ScriptingContainer is there. This
means multiple JSR223 engines share only one state. I’ll fix this soon
so that two engine instances will have individual state when
singlethread or threadsafe model is chosen. Of course, default
singleton model won’t do that.

-Yoko

When I do an eval on the respective engine (on their respective rb
Budyanto


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

I meant multiple JRuby instances in the same JVM. JRuby can have many
instances in one JVM, which is how multiple Rails apps are able to run
(or multiple instances of the same Rails app, if thread safety is
important).

On Sun, Mar 7, 2010 at 10:46 AM, Budyanto H. [email protected]
wrote:

  http://xircles.codehaus.org/manage_email


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

On Sun, Mar 7, 2010 at 11:46 AM, Budyanto H. [email protected]
wrote:

By Isolated instance of JRuby, do you mean separate JVMs? Or is there a
way to run multiple instances of JRuby within the same JVM by setting
system properties?

JRuby’s JSR223 implementation has three models to associate with Ruby
runtime (see
http://kenai.com/projects/jruby/pages/RedBridge#Context_Instance_Type
). When you choose singlethread or threadsafe, none of these are
default, you’ll get two isolated states on a single JVM (after the
fix). Perhaps, you are seeing just engine instance, but behind the
engine, Ruby runtime always works for you. All variable values must be
on Ruby runtime to be interpreted. Behavior of global variables might
be different from JSR223 reference implementation at
scripting.dev.java.net since RI of global variables was semantically
not correct.

However, I’m really interested in the reason you need two engine
instances. You are saying you need two Ruby runtimes at the same
time. This expectation will totally conflict with a classloader global
runtime
(http://old.nabble.com/Moving-to-a-classloader-global-runtime!-td27045093.html#a27045093),
which is a near(?) future runtime of JRuby. Even though it is a global
variable, even though you have multiple engine instances, the value
assigned to the same name will be the same on all engines in your
case. Since you are trying to instantiate them on a single
classloader, only one runtime will be there behind the scene.

Would you show us the reason of two engines? Probably, we should
consider such use case.

-Yoko


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Hi Yoko,

Did the fix just get committed? I was able to do something similar to
your example by using the singlethread or threadsafe context.

Budyanto

Hi,

I fixed this problem, so give it a try.
Sample code is:

import java.util.List;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class MultipleEngineStates {

private MultipleEngineStates() throws ScriptException {
    System.out.println("[" + getClass().getName() + "]");
    System.setProperty("org.jruby.embed.localcontext.scope",

“singlethread”);
ScriptEngineManager manager = new ScriptEngineManager();
List factories =
manager.getEngineFactories();
ScriptEngineFactory factory = null;
while (factories.iterator().hasNext()) {
factory = factories.iterator().next();
if (“ruby”.equals(factory.getLanguageName())) {
break;
}
}
ScriptEngine engine1 = factory.getScriptEngine();
ScriptEngine engine2 = factory.getScriptEngine();
engine1.put(“Value”, new String("engine1 "));
engine2.put(“Value”, new Double(-0.0149));
Object obj1 = engine1.eval(“puts $Value; $Value + 2010.to_s”);
Object obj2 = engine2.eval(“puts $Value; $Value + 2010”);
System.out.println(obj1 + ", " + obj2);
}

public static void main(String[] args) throws ScriptException {
    new MultipleEngineStates();
}

}

This outputs:
engine1
-0.0149
engine1 2010, 2009.9851

Make sure, you are using singlethread or threadsafe model.

-Yoko

On Sun, Mar 7, 2010 at 3:59 PM, Yoko H. [email protected] wrote:

engine, Ruby runtime always works for you. All variable values must be
variable, even though you have multiple engine instances, the value

Thanks


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Yes. I was using march 3rd version. I couldn’t get it to work before
because I wasn’t explicitly setting the context. After I set the context
it was working.

On Fri, Mar 12, 2010 at 3:09 PM, Budyanto H. [email protected]
wrote:

Did the fix just get committed? I was able to do something similar to
your example by using the singlethread or threadsafe context.

I pushed the change on March 10. Do you mean you could do on the older
JRuby than March 10?

-Yoko


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email