0
0
Flaskframework~20 mins

Teardown hooks in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Teardown Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a Flask teardown hook raises an exception?
Consider a Flask app with a teardown hook that raises an exception. What is the behavior of the app after a request finishes?
Flask
from flask import Flask
app = Flask(__name__)

@app.teardown_request
def teardown(exception):
    raise Exception('Error in teardown')

@app.route('/')
def index():
    return 'Hello World'

if __name__ == '__main__':
    app.run()
AThe exception is logged but does not affect the response sent to the client.
BThe exception causes the server to crash and no response is sent.
CThe exception modifies the response body to include the error message.
DThe exception is ignored silently without any logging.
Attempts:
2 left
💡 Hint
Think about how Flask handles errors in teardown hooks after the response is sent.
state_output
intermediate
2:00remaining
What is the output count of teardown calls after multiple requests?
Given this Flask app, how many times will the teardown function print 'Teardown called' after 3 requests to '/'?
Flask
from flask import Flask
app = Flask(__name__)

count = 0

@app.teardown_request
def teardown(exception):
    global count
    count += 1
    print('Teardown called')

@app.route('/')
def index():
    return 'Hi'

if __name__ == '__main__':
    app.run()
A0
B1
C3
D6
Attempts:
2 left
💡 Hint
Teardown hooks run once per request after the response.
📝 Syntax
advanced
2:00remaining
Which option correctly registers a teardown hook for the application context?
Select the correct way to register a teardown function that runs when the Flask application context ends.
A
@app.teardown_app_context
def teardown_appcontext(exception):
    print('App context teardown')
B
@app.teardown_request
def teardown_appcontext(exception):
    print('App context teardown')
C
@app.teardown
def teardown_appcontext(exception):
    print('App context teardown')
D
@app.teardown_appcontext
def teardown_appcontext(exception):
    print('App context teardown')
Attempts:
2 left
💡 Hint
Check the exact decorator name for application context teardown.
🔧 Debug
advanced
2:00remaining
Why does this teardown hook not run after a request?
Identify the reason why the teardown function below never executes after requests.
Flask
from flask import Flask
app = Flask(__name__)

@app.teardown_request
def teardown():
    print('Teardown executed')

@app.route('/')
def index():
    return 'Hello'

if __name__ == '__main__':
    app.run()
AThe decorator @app.teardown_request is invalid and should be @app.teardown.
BThe teardown function must accept an exception argument even if unused.
CThe function name 'teardown' is reserved and causes a conflict.
DTeardown functions only run on error responses, not successful ones.
Attempts:
2 left
💡 Hint
Check the function signature required by teardown_request hooks.
🧠 Conceptual
expert
2:00remaining
What is the main difference between @app.teardown_request and @app.teardown_appcontext?
Choose the statement that best describes the difference between these two Flask teardown decorators.
A@app.teardown_request runs after each request; @app.teardown_appcontext runs when the app context ends, which may be less frequent.
B@app.teardown_request runs only on errors; @app.teardown_appcontext runs after every request.
C@app.teardown_request runs before the request starts; @app.teardown_appcontext runs after the request ends.
D@app.teardown_request and @app.teardown_appcontext are aliases and behave identically.
Attempts:
2 left
💡 Hint
Think about the lifecycle of a request versus the application context.