Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to control the order that after_request handlers are executed?

Tags:

python

flask

I have two after_request handlers. In my case, I need one to fire before the next.

@app.after_request
    def after_request_check_something(response):
        # do something
        return response

@app.after_request
    def after_request_compress(response):
        # do something
        return response

In my case, I want compress to go first, then check_something. But they are firing in reverse.

If it matters, in my actual code, these two handlers are not consecutively declared like this. They are each in different modules that are installed at different times.

How can I control the order of execution?

like image 632
101010 Avatar asked Jan 03 '17 00:01

101010


People also ask

What is the Order of execution of a handlers?

Handlers are executed in the order they are defined in the handlers section, not in the order listed in the notify statement. Notifying the same handler multiple times will result in executing the handler only once regardless of how many tasks notify it.

How are handlers executed in a play?

By default, handlers run after all the tasks in a particular play have been completed. Notified handlers are executed automatically after each of the following sections, in the following order: pre_tasks, roles / tasks and post_tasks. This approach is efficient, because the handler only runs once, regardless of how many tasks notify it.

What are console control handlers in Linux?

Console Control Handlers. Each console process has its own list of control handler functions that are called by the system when the process receives a CTRL+C, CTRL+BREAK, or CTRL+CLOSE signal. Initially, the list of control handlers for each process contains only a default handler function that calls the ExitProcess function.

What happens if none of the handlers returns true?

If none of the handlers returns TRUE, the default handler is called. The function's dwCtrlType parameter identifies which control signal was received, and the return value indicates whether the signal was handled.


2 Answers

Flask's after_request hooks are indeed executed in the reversed order of their declaration. Conversely, before_request hooks are executed in the order of their declaration.

If after_request hooks are declared on a blueprint, those are executed in reversed declaration order, before global after_request hooks. If before_request hooks are declared on a blueprint, those are executed in declaration order, after global before_request hooks.

See preprocess_request and process_response source code for implementation details.

like image 103
user8808265 Avatar answered Oct 22 '22 05:10

user8808265


Edit: user8808265's answer looks more correct than mine.

From the documentation, it does not appear that the execution of these after_request handlers can be ordered. The implementation has the handlers stored in a dictionary which is inherently unordered.

I suggest making a separate handler that calls both in the correct order, something like:

def after_request_check_something(response):
    # do something
    return response


def after_request_compress(response):
    # do something
    return response

@app.after_request
def after_request_combined(response):
    response1 = after_request_compress(response)
    return after_request_check_something(response1)
like image 21
Frank T Avatar answered Oct 22 '22 04:10

Frank T