title: Python Flask, Handling Popen poll / wait / communicate without halting multi-threaded Python
body: <p>The code below is executed on a certain URL (/new...) and assigns variables to the session cookie, which is used to build the display. This example calls a command using subprocess.Popen.</p>
  
  <p>The problem is that the Popen command called below typically takes 3 minutes - and the subprocess.communicate Waits for the output - during which time all other Flask calls (e.g. another user connecting) are halted. I have some commented lines related to other things I've tried without success - one using the threading module and another using subprocess.poll.</p>
  
  <pre><code>from app import app
  from flask import render_template, redirect, session
  from subprocess import Popen, PIPE
  import threading
  
  
  @app.route('/new/&lt;number&gt;')
  def new_session(number):
      get_list(number)
      #t = threading.Thread(target=get_list, args=(number))
      #t.start()
      #t.join()
  
      return redirect('/')
  
  def get_list(number):
      #1 Call JAR Get String
      command = 'java -jar fetch.jar' + str(number)
      print "Executing " + command
      stream=Popen(command, shell=False, stdout=PIPE)
  
      #while stream.poll() is None:
      #    print "stream.poll = " + str(stream.poll())
      #    time.sleep(1)
      stdout,stderr = stream.communicate()
  
      #do some item splits and some processing, left out for brevity
      session['data'] = stdout.split("\r\n")
  
      return
  </code></pre>
  
  <p>What's the "better practice" for handling this situation correctly?</p>
  
  <p>For reference, this code is run in Python 2.7.8 on win32, including Flask 0.10.1</p>