title: Run and get output from a background process
body: <p>I already know there is several questions targeting this theme, but none of them solved my specific problem. Or at least I could not find it.</p>
  
  <p>I need to execute some program in background, wait for output and manipulate it. <strong>But the background program must keep executing</strong>.</p>
  
  <p>The info from the background program that I need is located, precisely, in the second line of its output. There is no problem if this program blocks my code until reach this line. But it is essential that it unlocks after that line so I can perform other tasks completely unrelated with the background program.  </p>
  
  <p>Still, I cannot figure out how to accomplish this. I've read a lot the documentation of <code>subprocess</code> module, in particular, <code>subprocess.Popen</code>.</p>
  
  <p>Being pratical: why <a href="https://stackoverflow.com/a/4417735/563154">this code</a> does not work with <code>['localtunnel', '8000']</code> argument? It outputs nothing...</p>
  
  <p>I know I do not need root privileges to execute this.</p>
  
  <hr>
  
  <p><strong>EDIT after answers from jadkik94 and fest</strong></p>
  
  <p>Unfortunately, both answers do not work for me. Maybe I am doing something wrong...</p>
  
  <p>First of all. A 'sanity check':</p>
  
  <pre><code>import subprocess, threading, time
  can_break = False
  
  def run():
      args = ["ping", "google.com"]
      popen = subprocess.Popen(args, shell=False, stdout=subprocess.PIPE)
      while not can_break:
          print popen.stdout.readline()
  
  t = threading.Thread(target=run)
  
  try:
      t.start()
      while True:
          print 'Main thread...'
          time.sleep(1)
  except KeyboardInterrupt:
      can_break = True
  </code></pre>
  
  <p>The code above works properly with output similar to this:</p>
  
  <pre><code>Main thread...
  PING google.com (74.125.234.160) 56(84) bytes of data.
  64 bytes from plusone.google.com (74.125.234.160): icmp_req=1 ttl=54 time=82.5 ms
  Main thread...
  64 bytes from plusone.google.com (74.125.234.160): icmp_req=2 ttl=54 time=82.7 ms
  [...]
  </code></pre>
  
  <p>But when I use it with the <code>args</code> I want (<code>args = ['localtunnel', 8000]</code>), the only output is <code>Main thread...</code>.</p>
  
  <p>When I call <code>localtunnel</code> in the main thread (blocking), it returns the desired output:</p>
  
  <pre><code>In [x]: popen = subprocess.Popen(['localtunnel', '8000'])
    This localtunnel service is brought to you by Twilio.
    Port 8000 is now publicly accessible from http://????.localtunnel.com ...
  </code></pre>
  
  <p>This approach is based on jadkik94's answer. But fest's answer does not work either.</p>