Python 2.7 で Twisted アプリケーションを構築しており、SqlAlchemy を使用してデータベースとやり取りしようとしています。動作中のアプリケーションでメモリ リークが発生していますが、リークを見つける方法がわかりません。「おそらくこれが問題です」として、SqlAlchemy の使用方法がリークの原因であるかどうかを尋ねています。データベースとの対話に使用するセッションを作成するためのデコレータを作成しました。デコレーターは、セッションをパラメーターとして受け入れる関数にラップされ、次のようにスレッドで呼び出されます。
@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)
ここで、process はデコレーターによってラップされた関数です。セッションを作成している私のデコレータコードは次のとおりです。
def db_scoped_session(engine, auto_commit=True):
''' Decorator: creates a SQLAlchemy Scoped Session and passes it in to the
method which can then pass it along to the code that
needs to operate with the db
Parameters:
engine a sqlalchemy configured engine to use
auto_commit if True, this will commit the session on successful
completion (no exceptions), will otherwise rollback
if False, assume the wrapped function is doing the
commit
'''
assert engine is not None, "Must pass a valid engine parameter"
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
私が上でやっていることに誰かが何か問題があると思いますか?
前もって感謝します!ダグ