I'm writing a script in Python that interacts with a jar file, the jar file at some point can ask for user input through stdin, however it can also print out an error.
For this I'm using subprocess.Popen
however I'm currently getting stuck when trying to read the output and error output, write to stdin if necessary and doing the same again.
However when using Popen.stderr.read()
it freezes as there is no error to read, and when using Popen.communicate()
to get the output, I can no longer send more data to the processes' STDIN.
How could I get around this issue? Here's my code:
from subprocess import Popen, PIPE
with Popen (["java","-jar","something.jar"], stdin=PIPE, stdout=PIPE, stderr=PIPE) as process:
# Closes STDIN, unable to communicate back
# stdout, stderr = process.communicate()
# Hangs forever if no STDERR is ever printed
# stdout = process.stdout.read()
# stderr = process.stderr.read()