How does Flask knows which decorated function to call?
As you can see in the code, the decorator adds the function you decorate with it to a list of functions (self.teardown_appcontext_funcs
) on a Flask
object instance (app
in your case). This list is then iterated over in reverse order when the appcontext is torn down, with each function being passed the exception triggering the teardown, or a placeholder exception if none was raised. This happens in Flask.do_teardown_appcontext()
.