librelist archives

« back to archive

teardown_appcontext and teardown_request

teardown_appcontext and teardown_request

From:
Jie Jiang
Date:
2014-06-26 @ 16:06
Hi all:

Would anyone kindly let me know the difference between teardown_appcontext
and teardown_request in Flask.

I have tried the following sample application, and it turns out they are
triggered the same time with any request
But I thought teardown_appcontext shall only be triggered when the
application is shutdown. Am I wrong with this assumption? And where is the
best place to place database disconnection code then?
Please advice! Much appreciated!

ps. it is running Flask 0.10.1 on vritualenv

# code:

from flask import Flask, current_app
import sys

app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello, I love it!"

@app.teardown_request
def teardown_request(exception):
    print >> sys.stderr, current_app.name + " request tear
down!!!!!!!!!!!!!"

@app.teardown_appcontext
def teardown_appcontext(exception):
    print >> sys.stderr, current_app.name + " appcontext tear
down!!!!!!!!!!!!!"

if __name__ == "__main__":
    app.run(port=8888)


# command line and output:
> python __init__.py
 * Running on http://127.0.0.1:8888/
__init__ request tear down!!!!!!!!!!!!!
__init__ appcontext tear down!!!!!!!!!!!!!
127.0.0.1 - - [26/Jun/2014 15:51:51] "GET / HTTP/1.1" 200 -
__init__ request tear down!!!!!!!!!!!!!
__init__ appcontext tear down!!!!!!!!!!!!!
127.0.0.1 - - [26/Jun/2014 15:52:02] "GET / HTTP/1.1" 200 -
__init__ request tear down!!!!!!!!!!!!!
__init__ appcontext tear down!!!!!!!!!!!!!


Best regards!

Jie Jiang

Re: [flask] teardown_appcontext and teardown_request

From:
Daniel Neuhäuser
Date:
2014-06-26 @ 16:19
The app context works like the request context but can also be used for 
situations like scripts in which there is no request to handle but you 
might still want current_app and g to work. 

If you are doing something request independent use the app context  (such 
as database access) otherwise use the request context. 

Re: [flask] teardown_appcontext and teardown_request

From:
Jie Jiang
Date:
2014-06-26 @ 23:11
Much appreciated Daniel! That explains well!
In this case, if do not want the database connection to be closed at the
end of each request, what would be the best place to put database
disconnection code?
Thank you!


Best regards!

Jie Jiang



2014-06-27 0:19 GMT+08:00 Daniel Neuhäuser <ich@danielneuhaeuser.de>:

> The app context works like the request context but can also be used for
> situations like scripts in which there is no request to handle but you
> might still want current_app and g to work.
>
> If you are doing something request independent use the app context  (such
> as database access) otherwise use the request context.
>

Re: [flask] teardown_appcontext and teardown_request

From:
Matthias Urlichs
Date:
2014-06-27 @ 01:37
Hi,

Jie Jiang:
> Much appreciated Daniel! That explains well!
> In this case, if do not want the database connection to be closed at the
> end of each request, what would be the best place to put database
> disconnection code?

What would you want to do with the database connection between requests?

If the answer is "nothing, I just want to save the setup cost", then the
database adapter is supposed to have a connection pool anyway, so you don't
care in your app.

-- 
-- Matthias Urlichs

Re: [flask] teardown_appcontext and teardown_request

From:
Jie Jiang
Date:
2014-06-27 @ 07:40
Thank you Matthias, that's the exact question I want to ask!
By the way, is there any good database adapter supporting connection pool
that I can use?



Best regards!

Jie Jiang



2014-06-27 2:37 GMT+01:00 Matthias Urlichs <matthias@urlichs.de>:

> Hi,
>
> Jie Jiang:
> > Much appreciated Daniel! That explains well!
> > In this case, if do not want the database connection to be closed at the
> > end of each request, what would be the best place to put database
> > disconnection code?
>
> What would you want to do with the database connection between requests?
>
> If the answer is "nothing, I just want to save the setup cost", then the
> database adapter is supposed to have a connection pool anyway, so you don't
> care in your app.
>
> --
> -- Matthias Urlichs
>

Re: [flask] teardown_appcontext and teardown_request

From:
Matthias Urlichs
Date:
2014-06-27 @ 15:17
Hi,

Jie Jiang:
> Thank you Matthias, that's the exact question I want to ask!
> By the way, is there any good database adapter supporting connection pool
> that I can use?
> 
flask-sqlalchemy of course. Just start from the example code.

--
-- Matthias Urlichs

Re: [flask] teardown_appcontext and teardown_request

From:
Jie Jiang
Date:
2014-06-29 @ 13:24
Thank you very much Daniel and Matthias, surely I will check it out.


Best regards!

Jie Jiang



2014-06-27 16:17 GMT+01:00 Matthias Urlichs <matthias@urlichs.de>:

> Hi,
>
> Jie Jiang:
> > Thank you Matthias, that's the exact question I want to ask!
> > By the way, is there any good database adapter supporting connection pool
> > that I can use?
> >
> flask-sqlalchemy of course. Just start from the example code.
>
> --
> -- Matthias Urlichs
>

Re: [flask] teardown_appcontext and teardown_request

From:
Daniel Neuhäuser
Date:
2014-06-27 @ 07:56
> By the way, is there any good database adapter supporting connection 
pool that I can use?

If you are going for relational databases just use SQLAlchemy. Even if you
don't want to use the ORM it's worth using.