Quantcast

A problem encountered when trying to invoke PythonInterpreter in different threads

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

A problem encountered when trying to invoke PythonInterpreter in different threads

huangyj1989
I encountered a problem when invoking a very simple python script using jython which embeded in a java application in different threads.

The jython version is 2.7.0

The PyRunner.java has the main method.

import org.python.core.Py;
import org.python.core.PySystemState;
import org.python.util.PythonInterpreter;

public class PyRunner {

private static PythonInterpreter createPythonInterpreter(String path) {
PySystemState state1 = new PySystemState();
state1.path.add(0, Py.newString(path));
state1.setCurrentWorkingDir(path);
return new PythonInterpreter(null, state1);
}

public static void main(String[] args) throws InterruptedException {
System.setProperty("python.home", "/usr/local/Cellar/jython/2.7.0/libexec");

Thread t = new Thread(() -> {
PyRunner pyRunner = new PyRunner();
pyRunner.run();
});
t.setName("python");
t.start();
// PyRunner pyRunner = new PyRunner();
// pyRunner.doWork();
Thread.sleep(5000l);

}

public void run() {
PythonInterpreter pythonInterpreter1 = createPythonInterpreter("the_path_to_my_python_script");
System.out.println("==============begin to execfile======================================");
pythonInterpreter1.execfile("Test.py");
System.out.println("==============end to execfile======================================");
}
}

The Test.py is as follows:

import ImportTest

ImportTest.testPrint('haha')
ImportTest.py is another file in the same path.

def testPrint(name):
print 'in test'

If I run the main method in PyRunner.java, I got the following error message

==============begin to execfile======================================
Exception in thread "python" Traceback (most recent call last):
  File "Test.py", line 1, in <module>
ImportError: No module named ImportTest

There are two wired thing that
1) if I run the execfile in the main thread, eg umcomment the two lines that currently commented, they will all work fine.
2) if I remove the parameter of the function testPrint, they will all work fine also.

I'm quiet new to jython, so is there anyone ever encounter such problem. Appreciate if you can reply.
Further more, I want to run code in different path in a single JVM, and for different interpreter only code in particular path can be executed. 
So using SystemState is the only way I can find to set the python path.

Best Regards,
Huang Yujie.


 


------------------------------------------------------------------------------
Developer Access Program for Intel Xeon Phi Processors
Access to Intel Xeon Phi processor-based developer platforms.
With one year of Intel Parallel Studio XE.
Training and support from Colfax.
Order your platform today. http://sdm.link/xeonphi
_______________________________________________
Jython-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jython-users
Loading...