Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Concurrent .jar Processes within Python

I have two .jar files that I want to call from a Python script. However, after I call the first jar, the terminal sits and doesn't process anything after that point as the server is running. The server starts fine, but I want to start another process that will run until I ask them to stop.

I've had trouble searching for possible solutions because I'm unsure of what terminology to use.

from subprocess import call 
import glob 
import sys 

h2 = glob.glob("h2*.jar")
reasoner = glob.glob("reasoner*.jar")

h2 = h2.pop()
reasoner = reasoner.pop()

call(["java", "-jar", h2, "-tcp"]) # Any call commands after this point don't execute
like image 789
AmericanKryptonite Avatar asked Jul 13 '26 04:07

AmericanKryptonite


1 Answers

Use subprocess.Popen instead of subprocess.call which wait the sub-process to terminate.

from subprocess import Popen

...

Popen(["java", "-jar", h2, "-tcp"])

FYI, Python documentation is good place to look, especially subprocess module documentation for this specific problem.


UPDATE

If you want to wait the sub-process explicitly when you're using Popen, save the reference to the Popen object and use wait method:

proc = Popen(["java", "-jar", h2, "-tcp"])
# Do something else ..
proc.wait()  # block execution until the sub-process terminate.
like image 143
falsetru Avatar answered Jul 14 '26 17:07

falsetru



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!