Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeatedly calling python from Java in the most efficient way

I have several python scripts (eg a.py,b.py) that share the same overhead at the top, eg:

import matplotlib.pylab as plt
#and some Oracle database connection and reading.

Then I have a java script that users Runtime to calls these several python scripts separately:

Process p = Runtime.getRuntime().exec("python a.py");
p.waitFor();
...
p = Runtime.getRuntime().exec("python b.py");
p.waitFor();

I suffer from the repeated overhead due to the java Runtime and python import overhead during each python call. I am wondering whether there is a way that involves only one time overhead during the first python call (by passing around some python handler/state maybe?) I am open to third party tools (eg Jython) as well.

like image 455
Hailiang Zhang Avatar asked May 30 '26 20:05

Hailiang Zhang


1 Answers

Not sure if this will speed up your program, but one option would be to use Jython to start up a Python interpreter once, and then re-use it for multiple scripts. You just need to add a dependency on the Jython JAR (download it at http://www.jython.org/downloads.html or use Maven/Gradle/etc.)

import org.python.util.PythonInterpreter;
public class JythonTest {
    public static void main(String[] args) {
        PythonInterpreter pythonInterpreter = new PythonInterpreter();
        pythonInterpreter.execfile("a.py");
        pythonInterpreter.execfile("b.py");
    }
}

Ref: http://tssblog.blogs.techtarget.com/2007/11/21/using-python-within-java/

like image 104
Stephen Rosenthal Avatar answered Jun 01 '26 10:06

Stephen Rosenthal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!