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.

The gist of the server is this:

def slow_messages():
  def generate():
    for i in range(60):
      print(f"Sending message {i} at {}")
      yield json.dumps({"message": f"Message {i}"}) + "\n"
    print(f"Sending done at {}")
    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 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 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?



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')