title: Python streaming std(in/out/err) to another process
body: <p>I have a python process running "in the background".  It's a <a href="http://docs.python.org/dev/library/subprocess.html#subprocess.Popen" rel="nofollow">subprocess.Popen()</a> call started in a totally different process.  As I've implemented it so far, the two processes are using <a href="http://docs.python.org/dev/library/multiprocessing.html#multiprocessing-listeners-clients" rel="nofollow">multiprocessing.connections.(Listener|Client)</a> to communicate with eachother.  But I'm open to any way to accomplish the following.</p>
  
  <p>What I want to do is have the "listener" background process use std(in|out|err) as if they've come from the "client" process.  Streaming is important so that io isn't read into memory but is read and written efficiently but also to avoid blocking on the listener process.  I guess that means I need a lower-level access or some sort of async/threading/multiprocessing solution.  That's what I'm unclear on how do to.  For example:</p>
  
  <pre><code>while True:  # TODO
      conn = listener.accept()
      try:
          callable_, args, kw = conn.recv()
          callable_(*args, **kw)
  </code></pre>
  
  <p>The question is how to allow callable_ to execute streaming to/from std(in|out|err) efficiently and without blocking.  The listener is also long-running, and I'm trying to support arbitrary python code that does <code>from sys import std(in|out|err)</code> at listener module import time will still see std(in|out|err) as if from the client. I can do setup (modifying/replacing sys.std(in|out|err)?) at listener startup before anything else is impored.</p>
  
  <p>Is there a good way to stream this io transparently such that after initial setup the code being run by the listener process can be arbitrary and can treat std(in|out|err) normally without blocking, etc.?  Preferably without creating new processes since the goal is to reduce process overhead, though using multiple processes on the client end is preferable since the client is thin.  Similarly, if I have to loop over reads/writes of segments of bytes from std(in|out|err) to accomplish effective streaming it would be best to do it in the client.</p>