Could anyone explain this line?
- From:
- chen ge
- Date:
- 2010-09-28 @ 05:35
Could anyone explain this line?Thanks.
g = LocalProxy(lambda: _request_ctx_stack.top.g)
code from flask
from werkzeug import LocalStack, LocalProxy
# context locals
_request_ctx_stack = LocalStack()
current_app = LocalProxy(lambda: _request_ctx_stack.top.app)
request = LocalProxy(lambda: _request_ctx_stack.top.request)
session = LocalProxy(lambda: _request_ctx_stack.top.session)
g = LocalProxy(lambda: _request_ctx_stack.top.g)
--
regards
chenge
Re: [flask] Could anyone explain this line?
- From:
- Jorge Vargas
- Date:
- 2010-09-28 @ 05:49
I'm not 100% sure as I haven't seen Flask code itself.
However this looks really close to optimizations I have seen in other
frameworks. Long story short this is a Stack Object Proxy in there
which is what does the magic for you to be able to do things like
having a global variable that is unique and thread safe for each
thread.
Therefore the above code is the one that shields you from having to
worry about two excecutions of the code corrupting each other's data.
On Tue, Sep 28, 2010 at 1:35 AM, chen ge <chenge2k@gmail.com> wrote:
> Could anyone explain this line?Thanks.
>
> g = LocalProxy(lambda: _request_ctx_stack.top.g)
>
> code from flask
>
> from werkzeug import LocalStack, LocalProxy
>
>
> # context locals
>
> _request_ctx_stack = LocalStack()
>
> current_app = LocalProxy(lambda: _request_ctx_stack.top.app)
>
> request = LocalProxy(lambda: _request_ctx_stack.top.request)
>
> session = LocalProxy(lambda: _request_ctx_stack.top.session)
>
> g = LocalProxy(lambda: _request_ctx_stack.top.g)
> --
> regards
>
> chenge
>
>
Re: [flask] Could anyone explain this line?
- From:
- Pitmairen
- Date:
- 2010-09-28 @ 07:39
When i use werkzeug i usually do it like this:
from werkzeug import Local, LocalManager
local = Local()
local_manager = LocalManager([local])
request = local('request')
What's the difference between this and the LocalStack approach?
On Tue, Sep 28, 2010 at 7:49 AM, Jorge Vargas <jorge.vargas@gmail.com>wrote:
> I'm not 100% sure as I haven't seen Flask code itself.
>
> However this looks really close to optimizations I have seen in other
> frameworks. Long story short this is a Stack Object Proxy in there
> which is what does the magic for you to be able to do things like
> having a global variable that is unique and thread safe for each
> thread.
>
> Therefore the above code is the one that shields you from having to
> worry about two excecutions of the code corrupting each other's data.
>
> On Tue, Sep 28, 2010 at 1:35 AM, chen ge <chenge2k@gmail.com> wrote:
> > Could anyone explain this line?Thanks.
> >
> > g = LocalProxy(lambda: _request_ctx_stack.top.g)
> >
> > code from flask
> >
> > from werkzeug import LocalStack, LocalProxy
> >
> >
> > # context locals
> >
> > _request_ctx_stack = LocalStack()
> >
> > current_app = LocalProxy(lambda: _request_ctx_stack.top.app)
> >
> > request = LocalProxy(lambda: _request_ctx_stack.top.request)
> >
> > session = LocalProxy(lambda: _request_ctx_stack.top.session)
> >
> > g = LocalProxy(lambda: _request_ctx_stack.top.g)
> > --
> > regards
> >
> > chenge
> >
> >
>
Re: [flask] Could anyone explain this line?
- From:
- Daniel Neuhäuser
- Date:
- 2010-09-28 @ 09:12
The `_request_ctx_stack` is a `LocalStack` of `RequestContext`s Flask
keeps, with every request one is pushed on the stack and at the end of
the request it's popped of the stack. Those contexts hold every
information global to a request like the current application, request,
session or the request globals.
Now as you can see a `LocalProxy` is used to make it possible to have an
API like `flask.g` or `flask.request`. What happens is that everytime
you access the proxy like `flask.request.form`, the proxy acquires a
lock, calls the passed function to get the object it proxies, returns
the appropriate attribute of the object it proxies and releases the
lock.
This design ensures that Flask works even in environments which are
using threads, greenlets etc.