If I try streaming a response from Flask, it works locally but not when deployed. There is a dupe question here: Stream in a Vercel Serverless function by Python (Flask) only working in local, but not once deployed · vercel · Discussion #2473 · GitHub which was locked. The answer suggests some nginx configuration, but I don’t see how that’s applicable for a basic Flask setup behind Vercel’s default proxies.
Here’s an example of streaming in Flask:
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.
Hi, @samastv!
I’m not familiar with streaming in Flask, but I can try and help out here. Maybe someone also in the community can jump in
If I use vercel dev
, the streaming doesn’t work and instead it waits for the full output to be generated.
Do you have any logs or error messages we can have a look at?
Hey @pawlean, thanks for the reply. There’s no abnormal logs or error messages since technically the app is able to serve my request successfully.
> [debug] [2024-08-14T05:23:36.941Z] Serving asset: [Lambda] api/index
> [debug] [2024-08-14T05:23:36.941Z] Invoking lambda: "api/index" with /find
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.