Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Flask application what is the difference between @app.teardown_request and @app.teardown_appcontext?

Tags:

python

flask

I was going through a flask application and found the use of @app.teardown_request. Can anyone please demystify the real reason of using @app.teardown_request rather than @app.teardown_appcontext?

In other words what exactly is the difference?

like image 772
Kazee Avatar asked Oct 23 '25 03:10

Kazee


1 Answers

@app.teardown_appcontext - Bind a function after each request, even if an exception is encountered.

@app.teardown_request - Registers a function to be called at the end of each request whether it was successful or an exception was raised. It is a good place to cleanup request scope objects like a database session/transaction.

https://github.com/pallets/flask-sqlalchemy/issues/379

like image 174
Raed Ali Avatar answered Oct 27 '25 01:10

Raed Ali



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!