Message8771
So this has an easy solution. We simply need to remove the ThreadLocal in PythonInterpreter#cleanup. Here's the full diff:
diff -r 4de0eff57af5 src/org/python/jsr223/PyScriptEngine.java
--- a/src/org/python/jsr223/PyScriptEngine.java Thu Jun 19 19:38:55 2014 +0100
+++ b/src/org/python/jsr223/PyScriptEngine.java Thu Jun 19 16:37:03 2014 -0600
@@ -17,7 +17,7 @@
import javax.script.SimpleBindings;
import org.python.util.PythonInterpreter;
-public class PyScriptEngine extends AbstractScriptEngine implements Compilable, Invocable {
+public class PyScriptEngine extends AbstractScriptEngine implements Compilable, Invocable, AutoCloseable {
private final PythonInterpreter interp;
private final ScriptEngineFactory factory;
@@ -231,4 +231,8 @@
return PyScriptEngine.this.eval(code, ctx);
}
}
+
+ public void close() {
+ interp.close();
+ }
}
diff -r 4de0eff57af5 src/org/python/util/PythonInterpreter.java
--- a/src/org/python/util/PythonInterpreter.java Thu Jun 19 19:38:55 2014 +0100
+++ b/src/org/python/util/PythonInterpreter.java Thu Jun 19 16:37:03 2014 -0600
@@ -25,7 +25,7 @@
* The PythonInterpreter class is a standard wrapper for a Jython interpreter
* for embedding in a Java application.
*/
-public class PythonInterpreter {
+public class PythonInterpreter implements AutoCloseable {
// Defaults if the interpreter uses thread-local state
protected PySystemState systemState;
@@ -352,6 +352,11 @@
} catch (PyException pye) {
// fall through
}
+ threadLocals.remove();
sys.cleanup();
}
+
+ public void close() {
+ cleanup();
+ }
}
I modified your test program slightly to use the try-with-resource form introduced by Java 7:
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.script.ScriptException;
import org.python.jsr223.PyScriptEngine;
import org.python.jsr223.PyScriptEngineFactory;
public class exhaust {
public static void main(String[] argv) {
ExecutorService pool = Executors.newSingleThreadExecutor();
int i = 0;
while (true) {
i++;
if (i % 10 == 0) {
System.out.println("Iteration " + i);
}
pool.submit(new Runnable() {
@Override
public void run() {
PyScriptEngineFactory factory = new PyScriptEngineFactory();
try (PyScriptEngine engine = (PyScriptEngine) factory.getScriptEngine()) {
engine.eval("s = 'hello' + ' world'");
} catch (ScriptException e) {
}
}
});
try {
Thread.sleep(10);
} catch (InterruptedException e) {
break;
}
}
}
}
Sorry it took so long for us to take a look! |
|
Date |
User |
Action |
Args |
2014-06-19 22:39:29 | zyasoft | set | messageid: <1403217569.3.0.499062632181.issue2026@psf.upfronthosting.co.za> |
2014-06-19 22:39:29 | zyasoft | set | recipients:
+ zyasoft, fwierzbicki, janvh |
2014-06-19 22:39:29 | zyasoft | link | issue2026 messages |
2014-06-19 22:39:29 | zyasoft | create | |
|