Forum: JRuby Per engine share variable

Posted by Budyanto Himawan (ngambek2003)
on 2010-03-06 00:21
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
Posted by Charles Nutter (headius)
on 2010-03-07 14:08
(Received via mailing list)
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 Himawan <lists@ruby-forum.com> 
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
Posted by Budyanto Himawan (ngambek2003)
on 2010-03-07 17:46
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
Posted by Yoko Harada (Guest)
on 2010-03-07 21:21
(Received via mailing list)
On Sun, Mar 7, 2010 at 8:07 AM, Charles Oliver Nutter
<headius@headius.com> 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
Posted by Charles Nutter (headius)
on 2010-03-07 21:52
(Received via mailing list)
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 Himawan <lists@ruby-forum.com> 
wrote:
>
>    http://xircles.codehaus.org/manage_email
>
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email
Posted by Yoko Harada (Guest)
on 2010-03-07 22:00
(Received via mailing list)
On Sun, Mar 7, 2010 at 11:46 AM, Budyanto Himawan <lists@ruby-forum.com> 
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
Posted by Yoko Harada (Guest)
on 2010-03-11 18:34
(Received via mailing list)
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<ScriptEngineFactory> 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 Harada <yokolet@gmail.com> 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
Posted by Budyanto Himawan (ngambek2003)
on 2010-03-12 21:09
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
Posted by Yoko Harada (Guest)
on 2010-03-12 22:58
(Received via mailing list)
On Fri, Mar 12, 2010 at 3:09 PM, Budyanto Himawan <lists@ruby-forum.com> 
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
Posted by Budyanto Himawan (ngambek2003)
on 2010-03-13 00:52
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.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.