A teardown hook in Flask is a function that runs after each request, used to clean up resources like closing database connections.
Click to reveal answer
beginner
How do you register a teardown hook in Flask?
You register a teardown hook using the @app.teardown_appcontext decorator for app-level cleanup or @app.teardown_request for request-level cleanup.
Click to reveal answer
intermediate
When exactly does a teardown hook run in Flask?
A teardown hook runs after the response is sent to the client, regardless of whether the request was successful or raised an error.
Click to reveal answer
intermediate
What argument does a teardown function receive in Flask?
A teardown function receives an error argument that is None if no error occurred, or contains the exception if one happened during the request.
Click to reveal answer
beginner
Why are teardown hooks important in Flask applications?
Teardown hooks help prevent resource leaks by ensuring things like database connections or files are properly closed after each request.
Click to reveal answer
Which decorator is used to register a teardown hook for the entire Flask application context?
A@app.route
B@app.teardown_appcontext
C@app.before_request
D@app.after_request
✗ Incorrect
The @app.teardown_appcontext decorator registers a function to run when the application context tears down.
When does a teardown hook run in Flask?
AAfter the response is sent
BOnly if the request is successful
COnly if the request raises an error
DBefore the request starts
✗ Incorrect
Teardown hooks run after the response is sent, regardless of success or error.
What argument does a teardown function receive?
AAn error object or None
BThe response object
CThe request object
DNo arguments
✗ Incorrect
Teardown functions receive an error argument that is None if no error occurred.
Which of these is a common use for teardown hooks?
AStarting a database connection
BLogging request start time
CClosing database connections
DRendering templates
✗ Incorrect
Teardown hooks are commonly used to close database connections or clean up resources.
Can teardown hooks modify the response sent to the client?
AOnly if an error occurred
BYes, always
COnly if no error occurred
DNo, they run after response is sent
✗ Incorrect
Teardown hooks run after the response is sent, so they cannot modify it.
Explain what a teardown hook is in Flask and why it is useful.
Think about what happens after a web request finishes.
You got /4 concepts.
Describe how to register a teardown hook in Flask and what argument the function receives.
Focus on the decorator and function signature.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of a teardown_request function in Flask?
easy
A. To handle incoming HTTP requests
B. To define URL routes
C. To start a new request
D. To run cleanup code after each request finishes
Solution
Step 1: Understand the role of teardown hooks
Teardown hooks run after a request ends to clean up resources like files or database connections.
Step 2: Match the purpose with options
Only To run cleanup code after each request finishes describes running cleanup code after each request finishes, which matches the teardown hook purpose.
Final Answer:
To run cleanup code after each request finishes -> Option D
Quick Check:
Teardown hooks = cleanup after request [OK]
Hint: Teardown hooks always run after request ends [OK]
Common Mistakes:
Confusing teardown with request handling
Thinking teardown runs before request
Assuming teardown defines routes
2. Which of the following is the correct way to register a teardown function in Flask?
easy
A. @app.teardown_request
def cleanup(error=None): pass
B. @app.after_request
def cleanup(): pass
C. @app.route('/teardown')
def cleanup(): pass
D. @app.before_request
def cleanup(): pass
Solution
Step 1: Identify the correct decorator for teardown
The correct decorator to register a teardown function is @app.teardown_request, which optionally accepts an error parameter.
Step 2: Check function signature
The function should accept an optional error=None parameter to detect errors during the request.
Final Answer:
@app.teardown_request\ndef cleanup(error=None): pass -> Option A
Quick Check:
Teardown decorator = @app.teardown_request [OK]
Hint: Teardown functions use @app.teardown_request with error param [OK]
Common Mistakes:
Using @app.before_request instead
Missing the optional error parameter
Confusing with @app.after_request
3. Given this Flask code snippet, what will be printed after a request completes successfully (no error)?
C. Missing the optional error parameter in the function
D. Function name must be 'teardown' exactly
Solution
Step 1: Check teardown function signature
Teardown functions must accept an optional error=None parameter to receive error info.
Step 2: Identify missing parameter issue
The function cleanup has no parameters, so Flask will raise an error when calling it.
Final Answer:
Missing the optional error parameter in the function -> Option C
Quick Check:
Teardown needs error param or it fails [OK]
Hint: Teardown functions must accept error=None parameter [OK]
Common Mistakes:
Omitting error parameter
Using wrong decorator
Thinking function name is fixed
5. You want to close a database connection after every request, but only if it was opened during that request. Which is the best way to do this using a teardown hook?
hard
A. Use @app.teardown_request with a function that checks if the connection exists and closes it
B. Use @app.before_request to close the connection before the request starts
C. Use @app.route to close the connection on a specific URL
D. Use @app.after_request to close the connection without checking if it exists
Solution
Step 1: Understand when to close resources
Resources like database connections should be closed after the request ends, not before or on specific routes.
Step 2: Use teardown_request with conditional check
The teardown function can check if the connection was opened during the request and close it safely.
Final Answer:
Use @app.teardown_request with a function that checks if the connection exists and closes it -> Option A
Quick Check:
Teardown + check connection before closing = best practice [OK]
Hint: Close resources in teardown only if they exist [OK]
Common Mistakes:
Closing connection before request starts
Closing without checking if connection exists
Trying to close on specific route only
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.