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.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With