Flask: return multiple responses incrementally from single request

Viewed 556

I have a flask server that does some very complex calculations, which can take up to 30 minutes to complete. I've written my code to yield the best possible result throughout the process. For example, my code may yield a top result at 5 min then an even better result at 20 min and then end the process at 30 min. Another example is it may return a result at 3 min and stop at 10 min.

I want to send these top results to a fronted visualization chart. Is there anyway to continuously stream data through a Flask API whenever I want to? Instead of the traditional single request, single response method.

I'd image the Flask route may look something like this:

@app.route("/stream", methods=["POST"])
def stream():
    curr_result = 0
    max_result = 0
    while True:
        curr_result = run_calculations(curr_result)
        if not curr_result:
            break
        if curr_result > max_result:
            max_result = curr_result
            yield max_result
1 Answers

Please see streaming for Flask. This is what you want to do.

https://flask.palletsprojects.com/en/1.1.x/patterns/streaming/

Sometimes you want to send an enormous amount of data to the client, much more than you want to keep in memory. When you are generating the data on the fly though, how do you send that back to the client without the roundtrip to the filesystem?

The answer is by using generators and direct responses.

Related