Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python SqlAlchemy在扭曲的线程中_Python_Multithreading_Sqlalchemy_Twisted - Fatal编程技术网

Python SqlAlchemy在扭曲的线程中

Python SqlAlchemy在扭曲的线程中,python,multithreading,sqlalchemy,twisted,Python,Multithreading,Sqlalchemy,Twisted,我正在用Python2.7构建一个Twisted应用程序,并尝试使用SqlAlchemy与数据库交互。我有一个正在工作的应用程序正在泄漏内存,不知道如何找到泄漏。也许这就是我要问的问题,我如何使用SqlAlchemy可能是泄漏的根源。我已经编写了一个decorator来创建用于与数据库交互的会话。decorator被包装在一个函数周围,该函数接受会话作为参数,并在如下线程中调用: @inlineCallbacks def update_db(data) @db_scoped_sessio

我正在用Python2.7构建一个Twisted应用程序,并尝试使用SqlAlchemy与数据库交互。我有一个正在工作的应用程序正在泄漏内存,不知道如何找到泄漏。也许这就是我要问的问题,我如何使用SqlAlchemy可能是泄漏的根源。我已经编写了一个decorator来创建用于与数据库交互的会话。decorator被包装在一个函数周围,该函数接受会话作为参数,并在如下线程中调用:

@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是由decorator包装的函数。下面是创建会话的装饰器代码:

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
有人看到我上面所做的有什么不对吗

提前谢谢! 道格