Execute a function after Flask returns response

The long story short is that Flask does not provide any special capabilities to accomplish this. For simple one-off tasks, consider Python’s multithreading as shown below. For more complex configurations, use a task queue like RQ or Celery.

Why?

It’s important to understand the functions Flask provides and why they do not accomplish the intended goal. All of these are useful in other cases and are good reading, but don’t help with background tasks.

Flask’s after_request handler

Flask’s after_request handler, as detailed in this pattern for deferred request callbacks and this snippet on attaching different functions per request, will pass the request to the callback function. The intended use case is to modify the request, such as to attach a cookie.

Thus the request will wait around for these handlers to finish executing because the expectation is that the request itself will change as a result.

Flask’s teardown_request handler

This is similar to after_request, but teardown_request doesn’t receive the request object. So that means it won’t wait for the request, right?

This seems like the solution, as this answer to a similar Stack Overflow question suggests. And since Flask’s documentation explains that teardown callbacks are independent of the actual request and do not receive the request context, you’d have good reason to believe this.

Unfortunately, teardown_request is still synchronous, it just happens at a later part of Flask’s request handling when the request is no longer modifiable. Flask will still wait for teardown functions to complete before returning the response, as this list of Flask callbacks and errors dictates.

Flask’s streaming responses

Flask can stream responses by passing a generator to Response(), as this Stack Overflow answer to a similar question suggests.

With streaming, the client does begin receiving the response before the request concludes. However, the request still runs synchronously, so the worker handling the request is busy until the stream is finished.

This Flask pattern for streaming includes some documentation on using stream_with_context(), which is necessary to include the request context.

So what’s the solution?

Flask doesn’t offer a solution to run functions in the background because this isn’t Flask’s responsibility.

In most cases, the best way to solve this problem is to use a task queue such as RQ or Celery. These manage tricky things like configuration, scheduling, and distributing workers for you.This is the most common answer to this type of question because it is the most correct, and forces you to set things up in a way where you consider context, etc. correctly.

If you need to run a function in the background and don’t want to set up a queue to manage this, you can use Python’s built in threading or multiprocessing to spawn a background worker.

You can’t access request or others of Flask’s thread locals from background tasks, since the request will not be active there. Instead, pass the data you need from the view to the background thread when you create it.

@app.route('/start_task')
def start_task():
    def do_work(value):
        # do something that takes a long time
        import time
        time.sleep(value)

    thread = Thread(target=do_work, kwargs={'value': request.args.get('value', 20)})
    thread.start()
    return 'started'

Leave a Comment