Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add shared memory between SubInterpreters #500

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/main/java/jep/Jep.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,13 @@ public Jep(JepConfig config) throws JepException {

protected Jep(JepConfig config, boolean useSubInterpreter, MemoryManager memoryManager)
throws JepException {
this(config, useSubInterpreter, memoryManager, false);
}

protected Jep(JepConfig config, boolean useSubInterpreter, MemoryManager memoryManager, boolean useSharedMemory)
throws JepException {
MainInterpreter mainInterpreter = MainInterpreter.getMainInterpreter();
if (threadUsed.get()) {
if (!useSharedMemory && threadUsed.get()) {
Thread current = Thread.currentThread();
StringBuilder warning = new StringBuilder(THREAD_WARN)
.append("Unsafe reuse of thread ").append(current.getName())
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/jep/JepConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
*/
package jep;

import jep.python.MemoryManager;

import java.io.File;
import java.io.OutputStream;
import java.util.Collections;
Expand Down Expand Up @@ -58,6 +60,8 @@ public class JepConfig {

protected Set<String> sharedModules = null;

protected MemoryManager sharedMemoryManager = null;

/**
* Sets a path of directories separated by File.pathSeparator that will be
* appended to the sub-intepreter's <code>sys.path</code>
Expand Down Expand Up @@ -201,4 +205,13 @@ public SubInterpreter createSubInterpreter() throws JepException {
return new SubInterpreter(this);
}

public SubInterpreter createSubInterpreterSharedMemory() {
if (sharedMemoryManager == null) {
SubInterpreter interpreter = new SubInterpreter(this);
sharedMemoryManager = interpreter.getMemoryManager();
return interpreter;
}
return new SubInterpreter(this, sharedMemoryManager, true);
}

}
6 changes: 6 additions & 0 deletions src/main/java/jep/SubInterpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
*/
package jep;

import jep.python.MemoryManager;

/**
* Class for creating instances of Interpreters which are sandboxed from other
* Interpreters. Sub-interpreters isolate different SubInterpreter instances,
Expand Down Expand Up @@ -63,4 +65,8 @@ public SubInterpreter(JepConfig config) throws JepException {
super(config);
}

public SubInterpreter(JepConfig config, MemoryManager memoryManager, boolean useSharedMemory) throws JepException {
super(config, true, memoryManager, useSharedMemory);
}

}
79 changes: 79 additions & 0 deletions src/test/java/jep/test/TestSharedMemoryInSubInterpreter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package jep.test;

import jep.Interpreter;
import jep.JepConfig;
import jep.JepException;

import java.util.Random;

public class TestSharedMemoryInSubInterpreter {
private JepConfig config;
private Interpreter mainIterp;
private Object pythonObject;

public static void main(String[] args) {
TestSharedMemoryInSubInterpreter app = new TestSharedMemoryInSubInterpreter();
app.init();

// test in single thread
int v = app.callMethodAdd(100, 50);
System.out.println("Add (100, 50) = " + v);

v = app.callMethodAdd(10, 10);
System.out.println("Sub (10, 10) = " + v);

// test with separate threads
Random random = new Random();

for (int i = 0; i < 3; i++) {
Thread thread = new Thread(() -> {
System.out.println(Thread.currentThread().getName() + ": started");
int a = random.nextInt(100);
int b = random.nextInt(100);

int r = app.callMethodAdd(a, b);
String result = a + " + " + b + " = " + r;

try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ": result(" + result + ")");
System.out.println(Thread.currentThread().getName() + ": finish");
});
thread.start();
}
}

private void init() throws JepException {
String initScript =
"class SimpleObject:\n" +
" def add(self, a, b):\n" +
" return a + b\n" +
" def sub(self, a, b):\n" +
" return a - b\n" +
"myobj = SimpleObject()";

// config need for sharing memory
config = new JepConfig();

Interpreter interp = config.createSubInterpreterSharedMemory();
interp.exec(initScript);
pythonObject = interp.getValue("myobj", Object.class);
// store main interpreter with shared memory
mainIterp = interp;
}

private int callMethodAdd(int a, int b) throws JepException {
// this interpreter accesses the object created in the main interpreter
try (Interpreter interp = config.createSubInterpreterSharedMemory()) {
interp.set("obj", pythonObject);
interp.set("a", a);
interp.set("b", b);
interp.exec("result = obj.add(a, b)");
return interp.getValue("result", Integer.class);
}
}

}