python - Why does SQLAlchemy/mysql keep timing out on me? -
i have 2 functions need executed , first takes 4 hours execute. both use sqlalchemy:
def first(): session = dbsession rows = session.query(mytable).order_by(mytable.col1.desc())[:150] i,row in enumerate(rows): time.sleep(100) print i, row.accession def second(): print "going onto second function" session = dbsession new_row = session.query(anothertable).order_by(anothertable.col1.desc()).first() print 'new row: ', new_row.accession first() second()
and here how define dbsession:
from sqlalchemy.ext.declarative import declarative_base sqlalchemy.orm import scoped_session, sessionmaker sqlalchemy import create_engine engine = create_engine('mysql://blah:blah@blah/blahblah',echo=false,pool_recycle=3600*12) dbsession = scoped_session(sessionmaker(autocommit=false, autoflush=false, bind=engine)) base = declarative_base() base.metadata.bind = engine
first() finishes fine (takes 4 hrs) , see "going onto second function" printed gives me error:
sqlalchemy.exc.operationalerror: (operationalerror) (2006, 'mysql server has gone away')
from reading docs thought assigning session=dbsession 2 different session instances , second() wouldn't timeout. i've tried playing pool_recycle , doesn't seem have effect here. in real world, can't split first() , second() 2 scripts: second() has execute after first()
assigning session=dbsession 2 different session instances
that isn't true. session = dbsession
local variable assignment, , cannot override local variable assignment in python (you can override instance member assignment, that's unrelated).
another thing note scoped_session produces, default, thread-local scoped session (i.e. codes in same thread have same session). since call first() , second() in same thread, 1 , same session.
one thing can use regular (unscoped) session, manage session scope manually , create new session in both function. alternatively, can check doc how define custom session scope.
Comments
Post a Comment