On Wed, Apr 6, 2016 at 12:57 PM, Jonathan Vanasco <jonat...@findmeon.com>

> 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
 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.

Reply via email to