title: Python. Continuous io eschange with subprocess.Popen() child process
body: <p>I made a simple model of what I am doing in a bigger application.</p>
  <p>I am trying to figure out how to communicate with the Popen process so that it is going to wait for io when required, and the user could provide that input. How to do it and is it even possible?</p>
  <p>Project files:</p>
  <p><strong>scanftest.c</strong></p>
  <pre><code>#include &lt;stdio.h&gt;
  
  int main(void) {
      
      int x = 1;
      
      printf(&quot;Please, enter integer to echo.\n&quot;);
  
      scanf(&quot;%d&quot;, &amp;x);
      
      printf(&quot;%d\n&quot;, x);
      
      return 0;
  }
  </code></pre>
  <p><strong>minirunner.py</strong></p>
  <pre><code>from subprocess import *  # run, Popen, PIPE
  
  run(&quot;gcc -o scanftest scanftest.c&quot;, shell=True)
  
  x = Popen(&quot;./scanftest&quot;, stdin=PIPE, stdout=PIPE, stderr=PIPE)
  
  while True:
  
      if x.returncode:
          break
  
      x.stdin.write(input(&quot;stdin&gt; &quot;).encode('utf-8'))
  
      print(x.stdout.read().decode('utf-8'))
  
  print(&quot;Done&quot;)
  
  </code></pre>
  <p>When I run <strong>minirunner.py</strong>, this is what happens:</p>
  <pre><code>stdin&gt; 10
  </code></pre>
  <p>I then press <code>^C</code> and see the following</p>
  <pre><code>^CTraceback (most recent call last):
    File &quot;minirunner.py&quot;, line 14, in &lt;module&gt;
      print(x.stdout.read().decode('utf-8'))
  KeyboardInterrupt
  
  </code></pre>
  <p>Seems like it is stuck when trying to <code>read()</code></p>
  <p>Meanwhile I expected and desired something like that:</p>
  <pre><code>stdin&gt; 10
  Please, enter integer to echo.
  stdin&gt; 10
  10
  Done.
  </code></pre>
  <p>Potentially I would like to deal with <code>scanf</code> in loops. But as You can see, even simple example fails.</p>