I have multiple clients trying to connect to a server sent events stream at /stream
. This works with a single client, but attempting to connect any more clients results in the new client becoming indefinitely blocked waiting for data. If I send more data, it only goes to the first client, and no others.
Here is a small snippet that illustrates my problem:
import flask
import time
app = flask.Flask(__name__)
def event_stream():
for i in xrange(9999):
yield "data: %d\n\n" % i
time.sleep(1)
@app.route("/stream", methods=[ "GET" ])
def stream():
return flask.Response(
event_stream(),
mimetype="text/event-stream"
)
I then run this with gunicorn --worker-class=gevent -w 4 -t 99999 app:app
. It works for a single client, but any others get blocked when issuing GET /stream
.
What is the cause of the block, and how should I fix it?
I debugged a little more and got some strange results. If I do this procedure, then this happens:
- Start client 1 (only client 1 receiving data)
- Start client 2 (only client 1 receiving data)
- Start client 3 (only client 1 receiving data)
- Start client 4 (only client 1 receiving data)
- Restart client 1 (all 4 clients suddenly start receiving data at the same time)