0
0
Flaskframework~10 mins

Teardown hooks in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Teardown hooks
Request starts
Before request handlers
View function runs
Response generated
Teardown hooks run
Request ends
This flow shows how Flask runs teardown hooks after the response is ready, to clean up resources.
Execution Sample
Flask
from flask import Flask, g
app = Flask(__name__)

@app.teardown_request
def teardown(exception=None):
    print('Teardown hook runs')
This code registers a teardown hook that runs after each request to perform cleanup.
Execution Table
StepEventExceptionActionOutput
1Request startsNoneStart processing requestNo output
2View function runsNoneProcess request logicResponse created
3Response readyNonePrepare to send responseResponse sent
4Teardown hook runsNoneRun teardown_request functionPrints 'Teardown hook runs'
5Request endsNoneCleanup completeNo output
💡 Request processing finished, teardown hooks executed after response sent
Variable Tracker
VariableStartAfter Step 2After Step 4Final
exceptionNoneNoneNoneNone
gEmptyMay hold request dataCleared or unchangedEmpty or reset
Key Moments - 2 Insights
Why does the teardown hook run even if the view function raises an error?
Because teardown hooks run after the response or error handling, as shown in step 4 of the execution_table, ensuring cleanup always happens.
Can the teardown hook access the exception that caused the error?
Yes, the exception parameter in the teardown function receives the error if any, as shown in the variable 'exception' in variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is printed during the teardown hook step?
A'Teardown hook runs'
B'Request starts'
C'Response created'
D'Cleanup complete'
💡 Hint
Check the 'Output' column at step 4 in the execution_table.
At which step does the response get sent to the client?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Event' and 'Action' columns in the execution_table for when the response is sent.
If an exception occurs in the view function, what value does the 'exception' variable have in the teardown hook?
AA string 'error'
BNone
CThe exception object
DIt is undefined
💡 Hint
Refer to the 'exception' variable in variable_tracker and the teardown function parameter.
Concept Snapshot
Flask teardown hooks run after each request finishes.
Use @app.teardown_request decorator.
They receive an optional exception argument.
Useful for cleaning resources like DB connections.
Run even if errors occur in the view.
Executed after response is sent.
Full Transcript
In Flask, teardown hooks are functions that run after each request finishes processing. They are registered using the @app.teardown_request decorator. These hooks run after the response is sent to the client, allowing you to clean up resources such as database connections. The teardown function receives an optional exception argument that contains any error raised during the request. This ensures cleanup happens even if the view function raises an error. The execution flow starts with the request, runs the view, sends the response, then runs teardown hooks before ending the request.