Re: _app_ctx_stack - general question
- From:
- Aaron Rothenberg
- Date:
- 2012-10-09 @ 02:28
That's perfect.
Thanks
> Date: Mon, 8 Oct 2012 19:23:07 -0700
> From: kyle@bucebuce.com
> Subject: Re: [flask] _app_ctx_stack - general question
> To: flask@librelist.com
>
> Hey Arron,
>
> The ctx_stack is only available during a request.
>
> I wrote a little example here: https://gist.github.com/3856206
>
> See http://flask.pocoo.org/docs/design/#thread-locals for a little
> more info on these types of proxy objects.
>
> - Kyle
>
>
>
> On Mon, Oct 8, 2012 at 7:00 PM, Aaron Rothenberg
> <a_rothenberg@hotmail.com> wrote:
> > I was walking through this tutorial
> > http://flask.pocoo.org/docs/extensiondev/
> > and rewriting it for use with psycopg2. Not the in-memmorry sqlite DB.
> > here is the book code where it fails.
> >
> > from flask import _app_ctx_stack as stack
> >
> > class postgresql_wrap(object):
> >
> > ...
> >
> > ...
> >
> > @property
> >
> > def connection(self):
> > ctx = stack.top
> >
> > if ctx is not None:
> >
> > #return a connection
> >
> >
> > the stack.top is always None. I don't know how or where it gets set. I don't
> > need to use this stack. I'm just curios about it.
> > http://flask.pocoo.org/docs/extensiondev/
> >
> > thanks.
> >
> >
> >
> >
> >
> >
Re: _app_ctx_stack - general question
- From:
- Kyle Jones
- Date:
- 2012-10-09 @ 02:23
Hey Arron,
The ctx_stack is only available during a request.
I wrote a little example here: https://gist.github.com/3856206
See http://flask.pocoo.org/docs/design/#thread-locals for a little
more info on these types of proxy objects.
- Kyle
On Mon, Oct 8, 2012 at 7:00 PM, Aaron Rothenberg
<a_rothenberg@hotmail.com> wrote:
> I was walking through this tutorial
> http://flask.pocoo.org/docs/extensiondev/
> and rewriting it for use with psycopg2. Not the in-memmorry sqlite DB.
> here is the book code where it fails.
>
> from flask import _app_ctx_stack as stack
>
> class postgresql_wrap(object):
>
> ...
>
> ...
>
> @property
>
> def connection(self):
> ctx = stack.top
>
> if ctx is not None:
>
> #return a connection
>
>
> the stack.top is always None. I don't know how or where it gets set. I don't
> need to use this stack. I'm just curios about it.
> http://flask.pocoo.org/docs/extensiondev/
>
> thanks.
>
>
>
>
>
>
Re: _app_ctx_stack - general question
- From:
- Aaron Rothenberg
- Date:
- 2012-10-09 @ 20:23
If you access the "connection" property multiple times while the
context is still alive (for instance, during a web request), it will
only create a new connection once, and then return that same
connection the next time the property is accessed. Does that clear
things up a bit?
- KyleNo, I get that. It's the teardown that occurs at the end of the with
block which confuses me. At the end of the with block the teardown handles
will be executed
automatically. why add this if statement? > def teardown(self,
exception):> > ctx = stack.top> > if hasattr(ctx, 'sqlite3_db'):> >
ctx.sqlite3_db.close()> >I probably shouldn't take up band width on this
so I'll figure it out. Thanks. You guys have been a lot of help.
> Date: Tue, 9 Oct 2012 12:56:34 -0700
> From: kyle@bucebuce.com
> Subject: Re: [flask] _app_ctx_stack - general question
> To: flask@librelist.com
>
> On Tue, Oct 9, 2012 at 12:45 PM, Aaron Rothenberg
> <a_rothenberg@hotmail.com> wrote:
> > Thanks. I'm still not 100% with the code.
> > http://flask.pocoo.org/docs/extensiondev/
> >
> > class SQLite3(object):
> >
> > def __init__(self, app=None):
> >
> > ......
> >
> >
> > def teardown(self, exception):
> > ctx = stack.top
> > if hasattr(ctx, 'sqlite3_db'):
> > ctx.sqlite3_db.close()
> >
> > .........
> >
> >
> > def connection(self):
> > ctx = stack.top
> > if ctx is not None:
> > if not hasattr(ctx, 'sqlite3_db'):
> > ctx.sqlite3_db = self.connect()
> > return ctx.sqlite3_db
> >
> >
> > When would the ctx not have "sqlite3_db"? When would the stack.top be
> > changed within the with block? I know I'm missing something obvious here.
> >
>
> If you access the "connection" property multiple times while the
> context is still alive (for instance, during a web request), it will
> only create a new connection once, and then return that same
> connection the next time the property is accessed. Does that clear
> things up a bit?
>
> - Kyle
>
>
>
>
>
> >
> >
> >
> >> Date: Tue, 9 Oct 2012 05:42:50 -0500
> >> From: armin.ronacher@active-4.com
> >
> >> Subject: Re: [flask] _app_ctx_stack - general question
> >> To: flask@librelist.com
> >>
> >> Hi,
> >>
> >> On 10/8/12 9:00 PM, Aaron Rothenberg wrote:
> >> > the stack.top is always None. I don't know how or where it gets set. I
> >> > don't need to use this stack. I'm just curios about it.
> >> > http://flask.pocoo.org/docs/extensiondev/
> >> The application context is set in two situations: if there is an active
> >> reques t context *or* if you set only the application context. The
> >
> >> latter is much faster because it does not have to set up a dummy HTTP
> >> request (new in 0.9). So you can do this:
> >>
> >> with app.app_context():
> >> do_stuff()
> >>
> >> (Instead of app.test_request_context(). It makes current_app and such
> >> things available, but not the request object, sessions or g)
> >>
> >>
> >> Regards,
> >> Armin
Re: _app_ctx_stack - general question
- From:
- Aaron Rothenberg
- Date:
- 2012-10-09 @ 19:45
Thanks. I'm still not 100% with the code.http://flask.pocoo.org/docs/extensiondev/
class SQLite3(object):
def __init__(self, app=None):......
def teardown(self, exception):
ctx = stack.top
if hasattr(ctx, 'sqlite3_db'):
ctx.sqlite3_db.close() ......... def connection(self):
ctx = stack.top
if ctx is not None:
if not hasattr(ctx, 'sqlite3_db'):
ctx.sqlite3_db = self.connect()
return ctx.sqlite3_db
When would the ctx not have "sqlite3_db"? When would the stack.top be
changed within the with block? I know I'm missing something obvious here.
> Date: Tue, 9 Oct 2012 05:42:50 -0500
> From: armin.ronacher@active-4.com
> Subject: Re: [flask] _app_ctx_stack - general question
> To: flask@librelist.com
>
> Hi,
>
> On 10/8/12 9:00 PM, Aaron Rothenberg wrote:
> > the stack.top is always None. I don't know how or where it gets set. I
> > don't need to use this stack. I'm just curios about it.
> > http://flask.pocoo.org/docs/extensiondev/
> The application context is set in two situations: if there is an active
> request context *or* if you set only the application context. The
> latter is much faster because it does not have to set up a dummy HTTP
> request (new in 0.9). So you can do this:
>
> with app.app_context():
> do_stuff()
>
> (Instead of app.test_request_context(). It makes current_app and such
> things available, but not the request object, sessions or g)
>
>
> Regards,
> Armin
Re: _app_ctx_stack - general question
- From:
- Kyle Jones
- Date:
- 2012-10-09 @ 19:56
On Tue, Oct 9, 2012 at 12:45 PM, Aaron Rothenberg
<a_rothenberg@hotmail.com> wrote:
> Thanks. I'm still not 100% with the code.
> http://flask.pocoo.org/docs/extensiondev/
>
> class SQLite3(object):
>
> def __init__(self, app=None):
>
> ......
>
>
> def teardown(self, exception):
> ctx = stack.top
> if hasattr(ctx, 'sqlite3_db'):
> ctx.sqlite3_db.close()
>
> .........
>
>
> def connection(self):
> ctx = stack.top
> if ctx is not None:
> if not hasattr(ctx, 'sqlite3_db'):
> ctx.sqlite3_db = self.connect()
> return ctx.sqlite3_db
>
>
> When would the ctx not have "sqlite3_db"? When would the stack.top be
> changed within the with block? I know I'm missing something obvious here.
>
If you access the "connection" property multiple times while the
context is still alive (for instance, during a web request), it will
only create a new connection once, and then return that same
connection the next time the property is accessed. Does that clear
things up a bit?
- Kyle
>
>
>
>> Date: Tue, 9 Oct 2012 05:42:50 -0500
>> From: armin.ronacher@active-4.com
>
>> Subject: Re: [flask] _app_ctx_stack - general question
>> To: flask@librelist.com
>>
>> Hi,
>>
>> On 10/8/12 9:00 PM, Aaron Rothenberg wrote:
>> > the stack.top is always None. I don't know how or where it gets set. I
>> > don't need to use this stack. I'm just curios about it.
>> > http://flask.pocoo.org/docs/extensiondev/
>> The application context is set in two situations: if there is an active
>> reques t context *or* if you set only the application context. The
>
>> latter is much faster because it does not have to set up a dummy HTTP
>> request (new in 0.9). So you can do this:
>>
>> with app.app_context():
>> do_stuff()
>>
>> (Instead of app.test_request_context(). It makes current_app and such
>> things available, but not the request object, sessions or g)
>>
>>
>> Regards,
>> Armin
Re: _app_ctx_stack - general question
- From:
- Armin Ronacher
- Date:
- 2012-10-10 @ 13:55
Hi,
On 10/9/12 2:45 PM, Aaron Rothenberg wrote:
> When would the ctx not have "sqlite3_db"? When would the stack.top be
> changed within the *with* block? I know I'm missing something obvious
> here.
teardown handlers are always executed, even if an exception happens. So
if some exception happens it won't be there. Likewise if you open
resource on first usage and nothing used that resource the request it
won't be there either.
Regards,
Armin
Re: _app_ctx_stack - general question
- From:
- Armin Ronacher
- Date:
- 2012-10-09 @ 10:42
Hi,
On 10/8/12 9:00 PM, Aaron Rothenberg wrote:
> the stack.top is always None. I don't know how or where it gets set. I
> don't need to use this stack. I'm just curios about it.
> http://flask.pocoo.org/docs/extensiondev/
The application context is set in two situations: if there is an active
request context *or* if you set only the application context. The
latter is much faster because it does not have to set up a dummy HTTP
request (new in 0.9). So you can do this:
with app.app_context():
do_stuff()
(Instead of app.test_request_context(). It makes current_app and such
things available, but not the request object, sessions or g)
Regards,
Armin
Re: _app_ctx_stack - general question
- From:
- Aaron Rothenberg
- Date:
- 2012-10-10 @ 19:50
Thanks. Kyle sent the same answer offline. It all makes sense now. Thanks.
> Date: Wed, 10 Oct 2012 08:55:19 -0500
> From: armin.ronacher@active-4.com
> Subject: Re: [flask] _app_ctx_stack - general question
> To: flask@librelist.com
>
> Hi,
>
> On 10/9/12 2:45 PM, Aaron Rothenberg wrote:
> > When would the ctx not have "sqlite3_db"? When would the stack.top be
> > changed within the *with* block? I know I'm missing something obvious
> > here.
> teardown handlers are always executed, even if an exception happens. So
> if some exception happens it won't be there. Likewise if you open
> resource on first usage and nothing used that resource the request it
> won't be there either.
>
>
> Regards,
> Armin