A more general solution than the flask iterator solution is to write a WSGI middleware that adds a callback to the response close method. Here we use the werkzeug ClosingIterator helper and a flask app extension to achieve this:
import traceback
from werkzeug.wsgi import ClosingIterator
class AfterResponse:
def __init__(self, app=None):
self.callbacks = []
if app:
self.init_app(app)
def __call__(self, callback):
self.callbacks.append(callback)
return callback
def init_app(self, app):
# install extension
app.after_response = self
# install middleware
app.wsgi_app = AfterResponseMiddleware(app.wsgi_app, self)
def flush(self):
for fn in self.callbacks:
try:
fn()
except Exception:
traceback.print_exc()
class AfterResponseMiddleware:
def __init__(self, application, after_response_ext):
self.application = application
self.after_response_ext = after_response_ext
def __call__(self, environ, after_response):
iterator = self.application(environ, after_response)
try:
return ClosingIterator(iterator, [self.after_response_ext.flush])
except Exception:
traceback.print_exc()
return iterator
You can then use your after_response
decorator like this:
import flask
import time
app = flask.Flask("after_response")
AfterResponse(app)
@app.after_response
def after():
time.sleep(2)
print("after_response")
@app.route("/")
def home():
return "Success!\n"
When you curl this, you'll see that it responds immediately and curl closes, then 2s later your "after" message appears in the logs:
127.0.0.1 - - [25/Jun/2018 15:41:51] "GET / HTTP/1.1" 200 -
after_response
This answer is summarized from my answers here and here.