title: Long Polling in Python with Flask
body: <p>I'm trying to do long polling with JQuery and Python under the Flask Framework.</p>
  
  <p>Having done long polling before in PHP, I've tried to go about it in the same way:</p>
  
  <p>A script/function that has a while(true) loop, checking for changes periodically eg.every 0,5 seconds in the database, and returns some data when a change occurs.</p>
  
  <p>So in my <strong>ini</strong>.py I've created an app.route to /poll for JQuery to call. JQuery gives it some information about the client's current state, and the poll() function compares this with what's currently in the database. The loop is ended and returns information when a change is observed.</p>
  
  <p>Here's the python code:</p>
  
  <pre><code>@app.route('/poll')
  def poll():
  client_state = request.args.get("state")
  
      #remove html encoding + whitesapce from client state
      html_parser = HTMLParser.HTMLParser()
      client_state = html_parser.unescape(client_state)
      client_state = "".join(client_state.split())
  
      #poll the database
      while True:
          time.sleep(0.5)
          data = get_data()
          json_state = to_json(data)
          json_state = "".join(data) #remove whitespace
  
          if json_state != client_state:
              return "CHANGE"
  </code></pre>
  
  <p>The problem is that, when the code above starts polling, the server appears to be overloaded and other Ajax calls, and other requests like loading a "loading" image to the html using JQuery are unresponsive and timeout.</p>
  
  <p>For completion's sake I've included the JQuery here: </p>
  
  <pre><code>function poll() {
  
  queryString = "state="+JSON.stringify(currentState);
  
  $.ajax({
      url:"/poll",
      data: queryString,
      timeout: 60000,
      success: function(data) {
          console.log(data);
          if(currentState == null) {
              currentState = JSON.parse(data);
          }
          else {
              console.log("A change has occurred");
          }
  
          poll();
  
      },
      error: function(jqXHR, textStatus, errorThrown) {
  
          console.log(jqXHR.status + "," + textStatus + ", " + errorThrown);
  
          poll();
  
      }
  });
  
  }
  </code></pre>
  
  <p>Does this need to multi-threaded or something? Or does anyone have any idea why I'm experiencing this behavior?</p>
  
  <p>Thanks in advance!! :)</p>