0

Need to execute a python program using Python interpreter. The input parameter will be different for each invocation. Is there a way by which the Java Process / ProcessBuilder instances can be cached/reused so as to get optimal performance?

programmer
  • 175
  • 1
  • 9
  • You can reuse a `ProcessBuilder` instance and simply overwrite the command parameters by call the `command` method again. Keep in mind that the ProcessBuilder itself (not talking about the Processes started by it) is not threadsafe. Also, the overhead of creating a new ProcessBuilder is negligible compared to starting the actual external process. – codeflush.dev Jun 26 '20 at 17:14
  • Have you considered using _Jython_ ? Perhaps this Q&A is relevant? https://stackoverflow.com/questions/2671768/calling-python-from-java-through-scripting-engine-jython – Abra Jun 26 '20 at 18:55
  • cannot use Jython since a specific Python runtime has to be used in this case. – programmer Jun 27 '20 at 04:14

1 Answers1

1

The Javadoc tells that you can call the start() method multiple times. So the answer to your question is yes. But I would not do it, if that makes the program structure more complicated.

The ProcessBuilder object itself is very lightweight because it simply starts en external program (Python in this case). You will see that if you take a look into the source code of that class. The Python interpreter is what takes a lot of system resources.

Stefan
  • 1,645
  • 1
  • 10
  • 15
  • More than caching/reusing processbuilder, can the Python interpreter process itself be kept ready (or cached) in a pool, and can be fetched and used when required? This way it will help in speeding up the processing of the request. – programmer Jun 27 '20 at 04:16
  • I don't know for sure but I assume that this is not possible. – Stefan Jun 27 '20 at 19:52
  • @programmer sure thats possible. I have just done that for my company to get prediction results from ML Models written in Python through Java. I first used Jep (Java Embedded Python) but moved on to Py4J, which I'm currently using for that purpose. With Py4J you can start a Python Process and connect to it via Java. Both processes communicate via TCP locally. But Py4J hides that from you – codeflush.dev Jun 30 '20 at 08:14