Hi there
In Vert.x we want to run multiple JRuby scripts (using
ScriptingContainer) concurrently in the same JVM instance such that
they’re isolated - at the very least I don’t want them to be able to see
any top level variables or methods from other instances.
Up until now we’ve done this by each script running in its own
ScriptingContainer instance, e.g.:
ScriptingContainer container1 = new
ScriptingContainer(LocalContextScope.SINGLE_THREAD);
container1.runScriptlet(“script1.rb”);
ScriptingContainer container2 = new
ScriptingContainer(LocalContextScope.SINGLE_THREAD);
container2.runScriptlet(“script2.rb”);
This works fine, but we end up with each container having its own JRuby
runtime so there’s a big memory overhead which results in us not being
able to have more than a few hundred containers running at the same time
in the JVM.
I would like to somehow share a ScriptingContainer between the various
script instances so as to reduce the memory overhead.
I.e. do something like:
ScriptingContainer container = new ScriptingContainer();
container.runScriptlet(“script1.rb”);
container.runScriptlet(“script2.rb”);
I looked at the various context instance types here
The SINGLETON type is obviously not appropriate since there is only one
variable map and we want each script to be isolated.
The THREAD_SAFE type is not appropriate since it seems to tie the
runtime instance to the thread its being executed on - this means any
two scripts run with the same thread end up with the same variable map,
which breaks isolation.
The SINGLE_THREAD is not appropriate since container.runScriptlet might
be called from different threads.
The CONCURRENT type is not appropriate since it also seems to map the
scope to the thread its being executed on (similarly to THREAD_SAFE)
Basically I want to get an isolated variable map for every time I call
runScriptlet, but share a ruby runtime between them, but I can’t see any
way to do this using the API.
Any help greatly appreciated!