Within a contextual session, multiple queries all seem to return the same data 
even though with SQL logging I can see the data has been changed by another 
thread and a SELECT has been issued by the query.

I realize that I need to close or remove the session when I am finished, but 
shouldn't a query return the current value from the database?

I boiled it down to a simple test program.  I am not seeing what I'm missing.  
Thanks.

----

from sqlalchemy import *
from sqlalchemy.orm import *
import time, thread

class Thing(object):
        pass

def Collector():
        while 1:
                session = Session()
                
                for t in session.query(Thing):
                        t.tick += 1
                        print 'Thread C - %d' % t.tick
                
                session.commit()
                time.sleep(1)

def Analyzer():
        while 1:
                session = Session()
                
                print '----'
                for i in range(0,5):
                        for t in session.query(Thing):
                                print 'Thread A - %d' % t.tick
                        
                        time.sleep(1)
                
                session.close()

if __name__ == "__main__":
        
        engine = create_engine('sqlite:////tmp/thing.db', 
strategy='threadlocal', echo=False)
        mt = MetaData()
        mt.bind = engine

        Session = scoped_session(sessionmaker(bind=engine, autoflush=False, 
autocommit=False))

        tbl_thing = Table('tbl_thing', mt,
                Column('id', Integer, primary_key=True),
                Column('tick', Integer, default=0 ),
        )

        Session.mapper(Thing, tbl_thing, save_on_init=False)

        mt.create_all(engine)

        session = Session()
        
        t1 = Thing()
        session.add(t1)
        session.commit()
        
        thread.start_new_thread( Collector, () )
        thread.start_new_thread( Analyzer, () )
        
        while 1:
                time.sleep(1)



      


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