Re: [pylons-devel] correct way to close a sqlalchemy session?
On Wed, Apr 6, 2016 at 12:57 PM, Jonathan Vanasco <jonat...@findmeon.com> wrote: > I edited my original post before hitting submit, and managed to clear out > the important stuff. Ha. > > On Wednesday, April 6, 2016 at 2:42:38 PM UTC-4, Jeff Dairiki wrote: >> >> I use a reified request property to create the SqlAlchemy session. >> The factory function adds an "add_finished_callback" to close the session. > > > I do that in all my personal/work apps. > Right now, I'm using the stock scaffold with global scoped session for a > side project that I'm open sourcing. > > I'm not very familiar with the stock scaffold. I just went to look at it and was initially confused, since the scaffold in the master branch currently uses a reified request property to construct the SqlAlchemy session (without a global scoped session). (See models/__init__.py <https://github.com/Pylons/pyramid/blob/master/pyramid/scaffolds/alchemy/%2Bpackage%2B/models/__init__.py_tmpl>.) Maybe that's enough motivation to go that way? > >> (Why don't you have access to the request?) >> > > I'm trying to keep things simple and do this in app/__init__.py. > > the `main` function only has a config object, the request doesn't exist > yet. > > def main(global_config, **settings): > config = Configurator(settings=settings) > wsgi_app = config.make_wsgi_app() > return wsgi_app > > so I need to somehow use a hook that has a request object. two things > that came to mind: tweens and events. > > > def db_cleanup__tween_factory(handler, registry): > def db_cleanup__tween(request): > try: > response = handler(request) > return response > finally: > DBSession.close() > return db_cleanup__tween > > def db_cleanup__event(event): > event.request.add_finished_callback(lambda request: > DBSession.close()) > > def main(global_config, **settings): > ... > config.add_tween('app.db_cleanup__tween_factory', over=EXCVIEW) > config.add_subscriber("app.db_cleanup__event", > "pyramid.events.NewRequest") > ... > > Of course, either will work. FWIW, of the two, I'd vote for the tween — the logic seems clearer to me, not that either is a brain-twister. -- You received this message because you are subscribed to the Google Groups "pylons-devel" group. To unsubscribe from this group and stop receiving emails from it, send an email to pylons-devel+unsubscr...@googlegroups.com. To post to this group, send email to pylons-devel@googlegroups.com. Visit this group at https://groups.google.com/group/pylons-devel. For more options, visit https://groups.google.com/d/optout.
Re: [pylons-devel] correct way to close a sqlalchemy session?
I use a reified request property to create the SqlAlchemy session. The factory function adds an "add_finished_callback" to close the session. (Why don't you have access to the request?) On Wed, Apr 6, 2016 at 11:30 AM, Jonathan Vanascowrote: > A few routes I have need to explicitly commit the session, and require me > to use "long lasting sessions" ( > https://pypi.python.org/pypi/zope.sqlalchemy#long-lasting-session-scopes) > > Since this disables transaction's call to "close" on commit, what is the > best way to close the SqlAlchemy session? > > I ended up using a Tween, but my first thought was to use > "add_finished_callback" (but there was only a config object, not a request > object). > > -- > You received this message because you are subscribed to the Google Groups > "pylons-devel" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to pylons-devel+unsubscr...@googlegroups.com. > To post to this group, send email to pylons-devel@googlegroups.com. > Visit this group at https://groups.google.com/group/pylons-devel. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "pylons-devel" group. To unsubscribe from this group and stop receiving emails from it, send an email to pylons-devel+unsubscr...@googlegroups.com. To post to this group, send email to pylons-devel@googlegroups.com. Visit this group at https://groups.google.com/group/pylons-devel. For more options, visit https://groups.google.com/d/optout.
Re: Setting cookies in Pyramid for @action()s or HTTPFound redirects?
On Thu, Mar 10, 2011 at 01:07:32PM -0800, Seth wrote: Are those of us not returning real Response objects stuck with building our cookie strings manually, or am I missing something here? I think you should be able to use a sacrificial WebOb Response object to construct the cookies for you. Here's an untested example of what I mean: import webob res = webob.Response() res.set_cookie(cookie_name, cookie_value, path='/', max_age=max_age, httponly=True) request.response_headerlist = [ ('Set-Cookie', _) for _ in res.headers.getall('Set-Cookie') ] Jeff -- You received this message because you are subscribed to the Google Groups pylons-devel group. To post to this group, send email to pylons-devel@googlegroups.com. To unsubscribe from this group, send email to pylons-devel+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/pylons-devel?hl=en.
Re: Setting cookies in Pyramid for @action()s or HTTPFound redirects?
On Sat, Mar 12, 2011 at 01:55:21PM -0600, Michael Merickel wrote: Going through the trouble of creating a webob response is much more complicated than simply adding a response_callback. def _set_cookie(request, response): response.set_cookie() request.add_response_callback(_set_cookie) That does look like generally a better solution than mine. Thanks! -- You received this message because you are subscribed to the Google Groups pylons-devel group. To post to this group, send email to pylons-devel@googlegroups.com. To unsubscribe from this group, send email to pylons-devel+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/pylons-devel?hl=en.