Seems that once I defined a scoped session as:

Session = scoped_session(sessionmaker())

I can switch to a non scoped session simply calling:

sess=Session()

infact:

sess=Session
sess
<sqlalchemy.orm.scoping.ScopedSession object at 0x1debbd0>

sess=Session()
sess
<sqlalchemy.orm.session.Session object at 0x2383050>

is this the intended behaviuor?

thanks
drakkan

On 5 Ago, 11:27, drakkan <drakkan1...@gmail.com> wrote:
> Hi all,
>
> I'm trying to share an object with a thread, I already tryed to use
> Session.merge but I'm doing something wrong,
>
> here is a test case:
>
> from sqlalchemy import
> *
> from sqlalchemy.orm import
> *
>
> e = create_engine('postgres://postgres:postg...@127.0.0.1/test',
> echo=True)
> m = MetaData(e)
> t1 = Table('t1', m, Column('a', Integer, primary_key=True),
> Column('b', Integer))
>
> class A(object):
>      def __init__(self, a, b):
>          self.a = a
>          self.b = b
>
> mapper(A, t1)
>
> m.create_all()
>
> Session = scoped_session(sessionmaker())
>
> Session.add(A(1, 1))
> Session.commit()
>
> import threading
>
> def testthread(a):
>     a.a=2
>     Session.add(a)
>     Session.commit()
>
> a = Session.query(A).get(1)
> t=threading.Thread(target=testthread,args=(a,))
> t.start()
>
> and this is the generated expection:
>
> Traceback (most recent call last):
>   File "/usr/lib/python2.6/threading.py", line 525, in
> __bootstrap_inner
>     self.run()
>   File "/usr/lib/python2.6/threading.py", line 477, in run
>     self.__target(*self.__args, **self.__kwargs)
>   File "testthread.py", line 28, in testthread
>     Session.add(a)
>  .....
> InvalidRequestError: Object '<A at 0x2dbf390>' is already attached to
> session '47955344' (this is '47969872')
>
> what is the correct way to share object between different threads?
>
> regards
> drakkan
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to