title: Interactive, non-blocking subprocess.Popen script without using communicate or pexpect
body: <p>A: Why does it block?  </p>
  
  <p>B: How may I massage this <strong>slightly</strong> so that it will run without blocking?  </p>
  
  <pre><code>#!/usr/bin/env python
  import subprocess as sp
  import os
  
  kwds = dict(
      stdin=sp.PIPE,
      stdout=sp.PIPE,
      stderr=sp.PIPE,
      cwd=os.path.abspath(os.getcwd()),
      shell=True,
      executable='/bin/bash',
      bufsize=1,
      universal_newlines=True,
  )
  cmd = '/bin/bash'
  proc = sp.Popen(cmd, **kwds)
  proc.stdin.write('ls -lashtr\n')
  proc.stdin.flush()
  
  # This blocks and never returns
  proc.stdout.read()
  </code></pre>
  
  <p>I need this to run interactively.</p>
  
  <p>This is a simplified example, but the reality is I have a long running process and I'd like to startup a shell script that can more or less run arbitrary code (because it's an installation script).</p>
  
  <p>EDIT:
  I would like to effectively take a .bash_history over several different logins, clean it up so it is a single script, and then execute the newly crafted shell script line-by-line within a shell stored within a Python script.</p>
  
  <p>For example:</p>
  
  <pre><code>&gt; ... ssh to remote aws system ...
  &gt; sudo su -
  &gt; apt-get install stuff
  &gt; su - $USERNAME
  &gt; ... create and enter a docker snapshot ...
  &gt; ... install packages, update configurations
  &gt; ... install new services, update service configurations ...
  &gt; ... drop out of snapshot ...
  &gt; ... commit the snapshot ...
  &gt; ... remove the snapshot ...
  &gt; ... update services ...
  &gt; ... restart services ...
  &gt; ... drop into a tmux within the new docker ...
  </code></pre>
  
  <p>This takes hours manually; it should be automated.</p>