0

I have a python list of dicts that I parse to get value strings from specific keys. I need to send these strings to an executable jar that translates them then take the translation and add it back to the dict. The jar runs from the command line as:

java -jar myJar.jar -a

this opens

Enter a word to begin:

I can enter as many words as I want and it gives the translation. Then ctrl+Z+retrun to close.

I tried

    cmd = ['java', '-jar', 'myJar.jar', '-a']
    process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.PIPE, universal_newlines=True)
    stdout,stderr = process.communicate('word')

This works exactly once, then I have to call subprocess again. Is there a way to hold the jar open, translate a group of words and pipe the output to python? I have to do them one at a time; it's not possible to send a list or array.

1 Answer 1

1

'Popen.communicate' sends the input and then waits for end-of-file on output. By its documentation, it waits for the process to terminate before returning to its caller. Thus you cannot iteratively execute multiple 'communicate' calls.

You need to get the input and output streams from the process, and manage them yourself, rather than using 'communicate'. Loop, writing to the process input, and read from the process output.

Sign up to request clarification or add additional context in comments.

3 Comments

Alternately, find someone to modify the Java program so that it can take multiple lists and return multiple results. I'd use a FIFO for this, not the command line.
Per the description, the Java side is already reading from stdin and writing to stdout, not the command line. But I think (it's unclear) the Python side of this can't deliver multiple words, otherwise I'd suggest sending 'word1\nword2\nword3\n' down the pipe.
@marketplace - nonsense. Just look at what < | > do, in Unix/Linux shells and in Windows cmd.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.