pubsub.subscribe('chat')
for message in pubsub.listen():
    if i_should_close_the_connection:
        break
yield 'data: %s\n\n' % message['data']

@app.route('/stream')
def stream():
    return flask.Response(event_stream(), mimetype='text/event-stream')
How do I close a Server-Send Events connection in Flask?