Multiple Subprocess Communication

I am trying to pass input to a program open as a subprocess in Python. Using the communication () function does what I want, but it does it only once, and then waits for the subprocess to complete before allowing it to continue.

Is there a method or module similar to the communication () function in a function, but allows multiple communication with the child process?

Here is an example:

import subprocess p = subprocess.Popen('java minecraft_server.jar', shell=True, stdin=subprocess.PIPE); //Pipe message to subprocess' console here //Do other things //Pipe another message to subprocess' console here 

If this can be done easier without using a subprocess, that would be great too.

+6
python subprocess communication
source share
1 answer

You can write to p.stdin (and flush each time to make sure the data is actually sent) as many times as you want. The problem will only be if you want to return the results (since it is so difficult to convince other processes of not buffering their output!), But since you do not even set stdout= in your Popen class, that is clearly not a problem for you. (If the problem and you really need to defeat another process output buffering strategy, pexpect - or wexpect on Windows - the best solution - I recommend them very, very often on stackoverflow, but you don’t have URLs right now, so pls just look for them yourself if contrary to your example, you have this need).

+7
source share

All Articles