Sessions + Twisted Perspective Broker

47 views
Skip to first unread message

Jeff

unread,
Jul 18, 2008, 11:47:26 AM7/18/08
to sqlalchemy
Hello. First off--excellent work. I've been using SQLAlchemy for a
year or so now, and I must say it's pretty awesome, but that I'm still
learning it. So, on to my question.

I'm using Twisted's Perspective Broker to sit in between the database
on the server, and wxPython on the client. For those that don't know,
PB is used to translucently call remote functions and copy objects
back and forth across the network. I already have a method for making
SA objects "safe" to copy across the network--that's not an issue. I
don't think you need an understanding of Twisted or PB to help me out
with this, but if you need more information, or think I should send
this to Twisted's lists, please let me know.

When a client connects using PB, the server maintains a connection
with it (along with other data) in what's called an "Avatar". The
Avatar is used to deal with permissions and such, and in the case of
my app, it's used to manage preferences. Because I need frequent
access to the underlying Person object (from the person table), I
store it in the Avatar, as it's always available in my PB functions.

Then, when I need to access relations of it, I use object_session(),
so that I can query it. This works perfectly--most of the time.
Sometimes (as my app grows more complicated), I'll have issues with
object_session() returning None. I *think* what the problem is is
that I'm loading that Person row elsewhere (by searching for people,
for example), and that's pulling it out of the session that it was
originally in.

I think that I'm using sessions wrong. I know I should be creating a
new session for every request from PB-- but then how do I store a
persistent copy of the Person object in the Avatar? Should I create a
new session each time and use session.merge()? I should probably
*not* use contextual sessions, as, if I understand that correctly, it
wouldn't only help for a singly-threaded program. I don't need to
access the database asynchronously, so I think that makes things
simpler.

So, any advice?

Thanks,
Jeff

Michael Bayer

unread,
Jul 18, 2008, 2:12:54 PM7/18/08
to sqlal...@googlegroups.com

On Jul 18, 2008, at 11:47 AM, Jeff wrote:

> I think that I'm using sessions wrong. I know I should be creating a
> new session for every request from PB-- but then how do I store a
> persistent copy of the Person object in the Avatar? Should I create a
> new session each time and use session.merge()? I should probably
> *not* use contextual sessions, as, if I understand that correctly, it
> wouldn't only help for a singly-threaded program. I don't need to
> access the database asynchronously, so I think that makes things
> simpler.
>

if you have some kind of long running ORM-mapped instances that need
to participate in multiple sessions on an ad hoc basis, you use
merge() to get them in. It produces a copy of the object local to
that session, which wont be messed with by anything else. You can
only use contextual sessions if you've designed a contextual function
that interacts brilliantly with twisted's concurrency model. In this
case, such as, per-Avatar; assuming the Avatar corresponds to exactly
one single process of work (i.e. no concurrent access). You
definitely do *not* want to use the default thread local behavior
since Twisted implements concurrency without threads necessarily being
involved.

Jeff

unread,
Jul 18, 2008, 2:36:18 PM7/18/08
to sqlalchemy
Alright, merge() it is. That's kind of what I was leaning towards, as
it seems that it would be the easiest (at this point) to implement.
Thanks for weighing in, and thanks again for a great library.

-Jeff
Reply all
Reply to author
Forward
0 new messages