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