title: Communicate with python subprocess while it is running
body: <p>I running a subprocess that run a software in "command" mode. (This software is Nuke by The Foundy, in case you know that software)</p>
  
  <p>When in command mode, this software is waiting for user input. This mode allow to create compositing scripts without any UI.</p>
  
  <p>I have done this bit of code that start the process, find when the application is done starting then I try to send the process some commands, but the stdin doesn't seem to be sending the commands properly.</p>
  
  <p>Here the sample code I did to test this process.</p>
  
  <pre><code>import subprocess
  
  appPath = '/Applications/Nuke6.3v3/Nuke6.3v3.app/Nuke6.3v3' readyForCommand = False
  
  commandAndArgs = [appPath, '-V', '-t'] 
  commandAndArgs = ' '.join(commandAndArgs) 
  process = subprocess.Popen(commandAndArgs, 
                             stdin=subprocess.PIPE, 
                             stdout=subprocess.PIPE, 
                             stderr=subprocess.STDOUT, 
                             shell=True, )
  
  while True:
      if readyForCommand:
          print 'trying to send command to nuke...'
          process.stdin.write('import nuke')
          process.stdin.write('print nuke')
          process.stdin.write('quit()')
          print 'done sending commands'
          readyForCommand = False
      else:
          print 'Reading stdout ...'
          outLine = process.stdout.readline().rstrip()
          if outLine:
              print 'stdout:', outLine
  
              if outLine.endswith('getenv.tcl'):
                  print 'setting ready for command'
                  readyForCommand = True
  
      if outLine == '' and process.poll() != None:
          print 'in break!'
          break
  
  print('return code: %d' % process.returncode) 
  </code></pre>
  
  <p>when I run nuke in a shell and send the same commands here is what I get:</p>
  
  <pre><code>sylvain.berger core/$ nuke -V -t
  [...]
  Loading /Applications/Nuke6.3v3/Nuke6.3v3.app/Contents/MacOS/plugins/getenv.tcl
  &gt;&gt;&gt; import nuke
  &gt;&gt;&gt; print nuke
  &lt;module 'nuke' from '/Applications/Nuke6.3v3/Nuke6.3v3.app/Contents/MacOS/plugins/nuke/__init__.pyc'&gt;
  &gt;&gt;&gt; quit()
  sylvain.berger core/$ 
  </code></pre>
  
  <p>Any idea why the stdin is not sending the commands properly?
  Thanks</p>