Re: What's the best way to optimize database connections in Pyramid?

2011-03-13 Thread Jorgen Jorgensen
Damn(or hurrah), another new concept to learn - wikipedia: Reification
(computer science), making a data model for a previously abstract
concept

I simply can't wait till the day when a day gets some more hours to it...

fanx a lot!
jj

On Mon, Mar 14, 2011 at 1:02 AM, Michael Merickel  wrote:
> I've been a fan of tacking the connection onto the request object using
> @reify, thus if your request never calls "request.db" it doesn't have any db
> overhead.
> Thanks to reify, it reuses the same connection throughout that request. If
> you want to ensure that the connection is closed at the end of the request,
> you can also add a finished_callback to handle that.
> class MyRequest(Request):
>     @reify
>     def db(self):
>         # do stuff to get connection either from middleware in previous
> email
>         # or actually create a connection
>         conn =
>         # optional: add callback to cleanup after request is complete
>         def _cleanup(request):
>             conn.close()
>         self.add_finished_callback(_cleanup)
>         return conn
> The cleanup and things is handled by repoze.tm2 if using something like
> SQLAlchemy with the ZopeTransactionExtension, but otherwise you'll probably
> want to handle it yourself.
>
> Michael
>
>
> On Sun, Mar 13, 2011 at 8:54 AM, Fernando Correa Neto 
> wrote:
>>
>> Hi
>>
>> On Sun, Mar 13, 2011 at 5:54 AM, Seth  wrote:
>> [snip]
>> > How are other people connecting to a db with high-traffic sites using
>> > Pyramid and avoiding the static_route re-connection nonsense? Perhaps we
>> > could request to get a matched_route object on the NewRequest event
>> > object?
>>
>> I usually try to stick to a connection that is always on environ.
>> You'd have a wsgi middleware that always try to get the connection
>> from the threadinglocal and expose that under some environ key, say,
>> 'conn.mongo'.
>> If the middleware fails to get that from the thread, you'd create
>> another connection and then re-expose it under the same key.
>>
>> Regards,
>> Fernando
>>
>> --
>> 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.
>>
>
> --
> 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.
>



-- 
med venlig hilsen / with kind regards
Jørgen G. Jørgensen.
---

-- 
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: What's the best way to optimize database connections in Pyramid?

2011-03-13 Thread Jorgen Jorgensen
When failing to get it from the thread is that always because the
connection is closed or could it also be because some other thread had
locked the previously opened connection?



On Sun, Mar 13, 2011 at 9:54 PM, Fernando Correa Neto  wrote:
> Hi
>
> On Sun, Mar 13, 2011 at 5:54 AM, Seth  wrote:
> [snip]
>> How are other people connecting to a db with high-traffic sites using
>> Pyramid and avoiding the static_route re-connection nonsense? Perhaps we
>> could request to get a matched_route object on the NewRequest event object?
>
> I usually try to stick to a connection that is always on environ.
> You'd have a wsgi middleware that always try to get the connection
> from the threadinglocal and expose that under some environ key, say,
> 'conn.mongo'.
> If the middleware fails to get that from the thread, you'd create
> another connection and then re-expose it under the same key.
>
> Regards,
> Fernando
>
> --
> 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.
>
>



-- 
med venlig hilsen / with kind regards
Jørgen G. Jørgensen.
---

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