title: Flask + SSE : why is time.sleep() required?
body: <p>I'm building a flask app that will have a background processs (with <code>multiprocessing.Process</code>) polling a web service for new data, queueing the data so that a Server-Sent-Events endpoint can stream it to the client.</p>
  
  <p>The sample code is this:</p>
  
  <pre><code>#!/usr/bin/env python
  from __future__ import print_function
  import itertools
  import time
  from flask import Flask, Response, redirect, request, url_for
  
  from multiprocessing import Process, Queue
  
  def f(q):
      for i, c in enumerate(itertools.cycle('ABCDEFG')):
          time.sleep(1) 
          q.put( c )
  
  
  app = Flask(__name__)
  
  q = Queue()
  
  @app.route('/')
  def index():
      if request.headers.get('accept') == 'text/event-stream':
          def events():
              for i, c in enumerate(itertools.cycle('ABCDEFG')):
                  #yield "data: %s \n\n" % (q.get())
                  yield "data: %s \n\n" % (c)
                  time.sleep(0.01)
          return Response(events(), content_type='text/event-stream')
      return redirect(url_for('static', filename='index.html'))
  
  
  if __name__ == "__main__":
      p = Process(target=f, args=(q,))
      p.start()
  
      app.run(host='localhost', debug=True, port=23423)
  
      p.join()
  </code></pre>
  
  <p>The problem is that in Chrome, if I remove the <code>time.delay(0.01)</code> after the <code>yield</code>, the SSE seems to hang (client side).</p>