Re: [Web-SIG] Using decorators to add objects in a thread-local store..
On Tue, 15 Jul 2008 21:32:39 -0500 Ian Bicking <[EMAIL PROTECTED]> wrote: > Etienne Robillard wrote: > > > > Hi all, > > > > I'd like to have your input and comments on using decorators > > functions for adding extra options to the request.environ object. > > > > For instance, here's a decorator whichs adds a "scoped" session > > object into request.environ: > > > > def with_session(engine=None): > > """ > > Decorator function for attaching a `Session` instance > > as a keyword argument in `request.environ`. > > """ > > def decorator(view_func): > > def _wrapper(request, *args, **kwargs): > > scoped_session.set_session(engine) > > request.environ['_scoped_session'] = getattr(scoped_session, > > 'sessio > > You should always use a namespace, e.g., > request.environ['something._scoped_session'] = ... > > In the context of a Pylons controller you could do it this way. Of > course with just WSGI it would be better to wrap it via WSGI, which is > almost equivalent to a decorator: > > def with_session(engine=None): > def decorator(app): > def engine_wsgi_app(environ, start_response): > environ['...'] = ... > return app(environ, start_response) > return engine_wsgi_app > return decorator Tried, but its giving me headaches.. I think I will stick with the former... Plus, in webob, it is my assumption that Request objects returns a wsgi application with get_response(). This indicates most likely that both approaches are the same. :) > Pylons controllers aren't *quite* WSGI applications, but instances of > those controller classes are. So wrapping an individual controller with > middleware requires a bit more work. Ah. I just have a conflicting view regarding the definition of a 'middleware'. For me, middlewares shouldn't even exists, as I'm not even sure where they should fit in the WSGI pipeline.. In fact, I consider middlewares as a potential security hole... > > @with_session(engine=engine): > > def view_blog_list(request, *args, **kwargs): > > # get the local session object for this > > # request (thread-local) > > sess = request.environ['_scoped_session'] > > # do stuff with the Session object here... > > ... > > > > Is this a good approach, or can this be adapted to work > > in multithreaded environments ? > > Since you are passing around arguments to functions it should be fine in > threaded environments. Thanks. I'd definitely like to see more discussion regarding threads in the next version of WSGI. > -- > Ian Bicking : [EMAIL PROTECTED] : http://blog.ianbicking.org Kind Regards, Etienne ___ Web-SIG mailing list Web-SIG@python.org Web SIG: http://www.python.org/sigs/web-sig Unsubscribe: http://mail.python.org/mailman/options/web-sig/archive%40mail-archive.com
Re: [Web-SIG] Using decorators to add objects in a thread-local store..
On Jul 15, 4:44 pm, "Mike Orr" <[EMAIL PROTECTED]> wrote: > On Tue, Jul 15, 2008 at 1:42 PM, Etienne Robillard > > > > <[EMAIL PROTECTED]> wrote: > > > On Mon, 14 Jul 2008 16:09:18 -0400 > > Etienne Robillard <[EMAIL PROTECTED]> wrote: > > >> Hi all, > > >> I'd like to have your input and comments on using decorators > >> functions for adding extra options to the request.environ object. > > >> For instance, here's a decorator whichs adds a "scoped" session > >> object into request.environ: > > >> def with_session(engine=None): > >> """ > >> Decorator function for attaching a `Session` instance > >> as a keyword argument in `request.environ`. > >> """ > >> def decorator(view_func): > >> def _wrapper(request, *args, **kwargs): > >> scoped_session.set_session(engine) > >> request.environ['_scoped_session'] = getattr(scoped_session, > >> 'sessio > >> return view_func(request, *args, **kwargs) > >> return wraps(view_func)(_wrapper) > >> return decorator > > >> Then it can be used as follows: > > >> @with_session(engine=engine): > >> def view_blog_list(request, *args, **kwargs): > >> # get the local session object for this > >> # request (thread-local) > >> sess = request.environ['_scoped_session'] > >> # do stuff with the Session object here... > >> ... > > >> Is this a good approach, or can this be adapted to work > >> in multithreaded environments ? > > >> For details, you can checkout the source code of notmm, which > >> holds the current implementation of the with_session decorator: > > >> $ hg clone -r tiphttp://gthc.org/projects/notmm/repo/notmm > >> For more details about notmm, please see > >> here:http://gthc.org/projects/notmm/ > > >> Thanks and Regards, > > >> Etienne > > > Hi, > > > I'm forwarding this on pylons-discuss. I'd be interested in > > feedback on how to integrate SQLAlchemy in Pylons. Can this > > decorator (with_session) works on/with Pylons controllers too ? > > This is the "standard" > way.http://wiki.pylonshq.com/display/pylonsdocs/Using+SQLAlchemy+with+Pylons Ah yes. I forgot that document, it explains really closely what I was trying to do with the with_session decorator... Some notable differences: - myapp/model/meta.py: I just throw that stuff in a file named myapp/ config/environment.py. - myapp/model/__init__.py : Likewise, I defined a get_model() which is essentially a clone of init_model. It just returns a `Table` instance for a given table_name. > It puts a scoped session object at myapp.model.meta.Session Interesting. To put this in constrast with the with_session object, the only difference I see is the place to store and retrieve the Session object. (request.environ vs meta..) > I suppose the decorator would work, but it's not typical for an action > to read things directly from the environment unless it's something > Pylons doesn't support any other way. Well, I like refering to a web project by its name, rather than refering to it as a framework X project. For that reason I think 'typical' doesn't apply here, since supporting Pylons might not be it. I just read Pylons code for inspiration and technical guidance... :) > -- > Mike Orr <[EMAIL PROTECTED]> Thanks! Etienne ___ Web-SIG mailing list Web-SIG@python.org Web SIG: http://www.python.org/sigs/web-sig Unsubscribe: http://mail.python.org/mailman/options/web-sig/archive%40mail-archive.com
Re: [Web-SIG] Using decorators to add objects in a thread-local store..
Etienne Robillard wrote: Hi all, I'd like to have your input and comments on using decorators functions for adding extra options to the request.environ object. For instance, here's a decorator whichs adds a "scoped" session object into request.environ: def with_session(engine=None): """ Decorator function for attaching a `Session` instance as a keyword argument in `request.environ`. """ def decorator(view_func): def _wrapper(request, *args, **kwargs): scoped_session.set_session(engine) request.environ['_scoped_session'] = getattr(scoped_session, 'sessio You should always use a namespace, e.g., request.environ['something._scoped_session'] = ... In the context of a Pylons controller you could do it this way. Of course with just WSGI it would be better to wrap it via WSGI, which is almost equivalent to a decorator: def with_session(engine=None): def decorator(app): def engine_wsgi_app(environ, start_response): environ['...'] = ... return app(environ, start_response) return engine_wsgi_app return decorator Pylons controllers aren't *quite* WSGI applications, but instances of those controller classes are. So wrapping an individual controller with middleware requires a bit more work. return view_func(request, *args, **kwargs) return wraps(view_func)(_wrapper) return decorator Then it can be used as follows: @with_session(engine=engine): def view_blog_list(request, *args, **kwargs): # get the local session object for this # request (thread-local) sess = request.environ['_scoped_session'] # do stuff with the Session object here... ... Is this a good approach, or can this be adapted to work in multithreaded environments ? Since you are passing around arguments to functions it should be fine in threaded environments. -- Ian Bicking : [EMAIL PROTECTED] : http://blog.ianbicking.org ___ Web-SIG mailing list Web-SIG@python.org Web SIG: http://www.python.org/sigs/web-sig Unsubscribe: http://mail.python.org/mailman/options/web-sig/archive%40mail-archive.com
[Web-SIG] Using decorators to add objects in a thread-local store..
Hi all, I'd like to have your input and comments on using decorators functions for adding extra options to the request.environ object. For instance, here's a decorator whichs adds a "scoped" session object into request.environ: def with_session(engine=None): """ Decorator function for attaching a `Session` instance as a keyword argument in `request.environ`. """ def decorator(view_func): def _wrapper(request, *args, **kwargs): scoped_session.set_session(engine) request.environ['_scoped_session'] = getattr(scoped_session, 'sessio return view_func(request, *args, **kwargs) return wraps(view_func)(_wrapper) return decorator Then it can be used as follows: @with_session(engine=engine): def view_blog_list(request, *args, **kwargs): # get the local session object for this # request (thread-local) sess = request.environ['_scoped_session'] # do stuff with the Session object here... ... Is this a good approach, or can this be adapted to work in multithreaded environments ? For details, you can checkout the source code of notmm, which holds the current implementation of the with_session decorator: $ hg clone -r tip http://gthc.org/projects/notmm/repo/ notmm For more details about notmm, please see here: http://gthc.org/projects/notmm/ Thanks and Regards, Etienne ___ Web-SIG mailing list Web-SIG@python.org Web SIG: http://www.python.org/sigs/web-sig Unsubscribe: http://mail.python.org/mailman/options/web-sig/archive%40mail-archive.com