HTTP streaming in Flask doesn't work when app is deployed

I want to stream an HTTP response from a Flask server. It works well from the repl, but when deployed I don’t get any response until the full response is done.

https://replit.com/@mynameisjonasb/flask-streaming-repro#main.py

The gist of the server is this:

@app.route("/slow-messages")
def slow_messages():
  def generate():
    for i in range(60):
      print(f"Sending message {i} at {datetime.now().isoformat()}")
      yield json.dumps({"message": f"Message {i}"}) + "\n"
      time.sleep(1)
    print(f"Sending done at {datetime.now().isoformat()}")
    yield json.dumps({"message": "Done"}) + "\n"

  return app.response_class(generate(), content_type="application/x-ndjson")

When fetching with curl I get one message per line, once per second: curl https://flask-streaming-repro.mynameisjonasb.repl.co/slow-messages. Just as I want.

But when I deploy the app, I don’t get any responses for a minute, and then I get everything in one go. ( curl https://unwieldy-fortunate-observatory-mynameisjonasb.replit.app/slow-messages but I will probably undeploy it soon).

What’s going on here? Is there anything I can do to enable streaming HTTP reponses when the app is deployed?

Cheers,
Jonas

3 Likes

Hi @mynameisjonasb thanks for your message.

I’m sorry I’m not able to immediately help but I have invited @soren as this might relate to deployments.

I’ve also moved this to #support:bug-reports for the time being.

1 Like

I’m having the same issue. The following return statement gives me streaming HTTP in the development environment, but waits until the full response is done when deployed.

return Response(generate_response(input), mimetype='text/event-stream')