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