Re: [pylons-devel] correct way to close a sqlalchemy session?

2016-04-06 Thread Jonathan Vanasco


On Wednesday, April 6, 2016 at 8:14:02 PM UTC-4, Jeff Dairiki wrote:
>
>
> 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?
>

That's odd.  I did some digging in github, and that's tied to some 
forthcoming/development approaches on changing all the scaffolds..

The "classic" method is in the active branch (and leading up to it)

  
https://github.com/Pylons/pyramid/blob/1.6-branch/pyramid/scaffolds/alchemy/%2Bpackage%2B/models.py

 

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

I'll stick with the tween for now; it'll be too much work to convert to the 
'right' system.

I *did not* like using the scoped DBSession approach, but it matched the 
current Pyramid docs and tutorials.

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

2016-04-06 Thread Jeff Dairiki
On Wed, Apr 6, 2016 at 12:57 PM, Jonathan Vanasco 
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
.)
 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?

2016-04-06 Thread Jonathan Vanasco
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.

 

>   (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")
...




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

2016-04-06 Thread Jeff Dairiki
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 Vanasco 
wrote:

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