I'm building a Twisted application in Python 2.7 and am trying to use 
SqlAlchemy to interact with the database. I've got a working application 
that is leaking memory, and am not sure how to find the leaks. As a "maybe 
this is the problem" I'm asking if how I'm using SqlAlchemy might be the 
source of the leaks. I've written a decorator to create the sessions I use 
to interact with the database. The decorator is wrapped around a function 
that accepts the session as a parameter and is called in a thread like this:

@inlineCallbacks
def update_db(data)
    @db_scoped_session
    def process(session):
        # do SqlAlchemy work here

    # call process in thread
    result = yield threads.deferToThread(process)
defer.returnValue(result)
where process is the function wrapped by the decorator. Here is my 
decorator code that is creating the session:

def db_scoped_session(engine, auto_commit=True):
    def decorator(func):
        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            results = None
            db_sessionmaker = 
scoped_session(sessionmaker(expire_on_commit=True, bind=engine))
            db_session = db_sessionmaker()
            try:
                results = func(db_session, *args, **kwargs)
                # should we rollback for safety?
                if not auto_commit:
                    db_session.rollback()
            except:
                db_session.rollback()
                raise
            finally:
                db_session.commit()
            return results
        return wrapper
    return decorator

Does anyone see anything wrong with what I'm doing here?

Thanks in advance!
Doug

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to