title: Python Run a daemon sub-process & read stdout
body: <p>I need to run a program and gather its output to stdout. This program (socat) needs to run in the background for the duration of the python script. Socat sits in dameon mode once it's run, but first it outputs some lines to stdout that I need for the rest of my script.</p>
  
  <p>Command: <code>socat -d -d PTY: PTY:</code></p>
  
  <p>Output:</p>
  
  <pre><code>2011/03/23 21:12:35 socat[7476] N PTY is /dev/pts/1
  2011/03/23 21:12:35 socat[7476] N PTY is /dev/pts/2
  2011/03/23 21:12:35 socat[7476] N starting data transfer loop with FDs [3,3] and [5,5]
  </code></pre>
  
  <p>...</p>
  
  <p>I basically want to run that at the start of my program and leave it running till script termination, but I need to read the two /dev/pts/X names into python.</p>
  
  <p>Can anyone tell me how to do this?</p>
  
  <p>I came up with this which just hangs, I guess because it's blocking for the child process to terminate.</p>
  
  <pre><code>#!/usr/bin/python
  from subprocess import Popen, PIPE, STDOUT
  
  cmd = 'socat -d -d PTY: PTY: &amp;'
  
  p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)
  output = p.stdout.read()
  
  # Process the output 
  print(output)
  </code></pre>
  
  <p>Thanks for any help</p>
  
  <p>EDIT: Seems it may write to stderr, but the script still just hanges with and without the &amp; even reading from stderr.</p>