Flask socketio blocked by something during time.sleep
<p>I'm running a Flask server on my local machine and am using it to pull data from a serial port and display it in real time on a webpage with Flask-socketio. I followed the example <a href="https://github.com/miguelgrinberg/Flask-SocketIO/tree/master/example" rel="noreferrer">here</a> and am encountering an interesting behavior. I was able to replicate the results of the example, but when I alter the background_thread function to include reading and parsing data from the serial port, something seems to be blocking the socket from sending the data. My code correctly establishes a socket and opens the connection, and can even emit messages correctly at the beginning of the thread function, but once it hits a time.sleep(5), emitting doesn't work anymore. Here's the original thread function that works (and includes a time.sleep()!):</p>

<pre><code>def background_thread():
"""Example of how to send server generated events to clients."""
count = 0
while True:
    time.sleep(1)
    count += 1
    socketio.emit('my response',
                  {'data': 'Server generated event', 'count': count},
                  namespace='/test')
</code></pre>

<p>Here's a snip from my code. I'm using the delay to wait for my serial device to finish an operation before reading from it again.</p>

<pre><code>socketio.emit('my response', {'data': 'waiting'}, namespace='/test')
    print( "waiting...", file=sys.stderr)
    time.sleep(5)
    socketio.emit('my response', {'data': 'done sleeping'}, namespace='/test')
</code></pre>

<p>In this case, I see the "waiting" message, but not the "done sleeping" message or any other messages after that. There are no errors, the code keeps running correctly. However, when I hit ctrl+c to kill the script, suddenly all the messages that I tried to emit after the time.sleep() appear on the browser side. It's like something is blocking and buffering all the messages and then dumping them when the script is killed. I also tried to see if the amount of time was an issue and tried this:</p>

<pre><code> socketio.emit('my response', {'data': 'waiting'}, namespace='/test')
    print( "waiting...", file=sys.stderr)
    socketio.emit('my response', {'data': 'about to sleep'}, namespace='/test')
    time.sleep(1)
    socketio.emit('my response', {'data': 'sleep 1'}, namespace='/test')
    time.sleep(1)
    socketio.emit('my response', {'data': 'sleep 2'}, namespace='/test')
    time.sleep(1)
    socketio.emit('my response', {'data': 'sleep 3'}, namespace='/test')
    time.sleep(1)
    socketio.emit('my response', {'data': 'sleep 4'}, namespace='/test')
    time.sleep(1)
    socketio.emit('my response', {'data': 'done sleeping'}, namespace='/test')
</code></pre>

<p>In this case, I saw the "sleep 4" message, but nothing after that. Changing the number of sleep statements always resulted in it not emitting anything after the last sleep. To add to the mystery, if I remove the sleep statements entirely, I don't see anything get emitted from the thread at all. </p>

<p>I'm using the version 1.4.5 of Flask-socketio and eventlet as the async service. I tried replacing time.sleep() with eventlet.sleep(), but saw the same behavior. </p>