Communicate multiple times with a subprocess in Python
<p>This question is NOT a duplicate of </p>

<p><a href="http://stackoverflow.com/questions/3065060/communicate-multiple-times-with-a-process-without-breaking-the-pipe">Communicate multiple times with a process without breaking the pipe?</a></p>

<p>That question is solved because its use case allows inputs to be sent together, but this is not true if your program is interactive (as illustrated in the use case here).</p>

<hr>

<p>Document <code>subprocess.Popen</code> says:</p>

<pre><code>communicate(input=None)
    Interact with process: Send data to stdin.  Read data from stdout
    and stderr, until end-of-file is reached.  Wait for process to
    terminate.  ...
</code></pre>

<p>Is it possible to communicate multiple times with the subprocess before its termination, like with a terminal or with a network socket?</p>

<p>For example, if the subprocess is <code>bc</code>, the parent process may want to send it different inputs for calculation as needed. Since inputs send to <code>bc</code> may depend on user inputs, it is not possible to send all inputs at once.</p>

<p>Basicly <a href="http://stackoverflow.com/questions/375427/non-blocking-read-on-a-subprocess-pipe-in-python">Non-blocking read on a subprocess.PIPE in python</a></p>

<p>Set the proc pipes (proc.stdout, proc.stdin, ...) to nonblocking mode via <code>fnctl</code> and then write/read them directly.</p>

<p>You might want to use epoll or select via the <code>select</code> or <code>io</code> modules for more efficiency.</p>

python