from flask import Flask, Response
import time
app = Flask(__name__)
def generate_text():
for i in range(10):
yield "This is line %d.\n" % i
time.sleep(1)
@app.route('/')
def stream_text():
return Response(generate_text(), mimetype='text/plain')
if __name__ == '__main__':
app.run(debug=True)
If I directly use flask run, it works as expected. If I use vercel dev, the streaming doesn’t work and instead it waits for the full output to be generated.
where /find is the route I’m POSTing to. The functional difference I see is that when my results are being computed, I’m yielding results as I get them. In the example above, this is the
yield "This is line %d.\n" % i
step. When directly running my app with Flask locally, I see the webpage render the results in chunks, while running with vercel dev, the results are only rendered once the full evaluation is completed.
In some cases, the time it takes for computation may be longer than the timeout, so the user could see an error. In other cases where it is shorter than the timeout, the user still has to wait for a few seconds before seeing anything on the page. I was trying to circumvent this with streaming.