title: Start child process with subprocess.Popen and read its output while it is running
body: <p>I have a command whose output I am writing to a file and using <code>subprocess.Popen</code> to call the command. Here is a sample:</p>
  
  <pre><code>stdout_output = open('testfile.txt','w')
  process = subprocess.Popen(command,stdout=stdout_output,shell=True)
  stdout_read = open('testfile.txt','r')
  data = stdout_read.read()
  print data
  </code></pre>
  
  <p>The data sometimes has nothing in it but when I add some sleep between triggering the process and reading it for e.g </p>
  
  <pre><code>stdout_output = open('testfile.txt','w')
  process = subprocess.Popen(command,stdout=stdout_output,shell=True)
  time.sleep(3)
  stdout_read = open('testfile.txt','r')
  data = stdout_read.read()
  print data
  </code></pre>
  
  <p>Then the data contains the actual data written in the file. Is there something which I am missing here. Or is there any other way to add some time between triggering the process and reading the output instead of giving the hard-coded sleep.</p>
  
  <p><em>Note</em> - The command is an ongoing process. I cannot add <code>process.wait()</code> or cannot wait for the process to finish before reading the file.</p>