generator in flask

Found a way to yield values in flask. I wanted to send a large data, so this is useful. Here is a basic example.


from flask import Response

@app.route('/large.csv')
@app.route('/test')
def test():
    def generate():
        app.logger.info('request started')
        for i in range(5):
            time.sleep(1)
            yield str(i)
        app.logger.info('request finished')
        yield ''
    return Response(generate(), mimetype='text/plain')

To understand generator, yield and streaming, go over following materials

Bigliography

コメント