多线程scoped_session中的SQLAlchemy

时间:2015-04-16 16:55:50

标签: python sqlalchemy python-multithreading

我试图在多线程环境中使用SQLAlchemy,但我遇到了奇怪的错误。情况如下:

线程1启动所有SQLAlchemy对象(Engine,Models,scoped_session等)

然后,线程2尝试使用scoped_session对象在数据库上进行查询。不幸的是,从线程2中抛出了错误。

我创建了一个简单的测试用例来说明我想要完成的任务:

import sqlalchemy
from sqlalchemy import Column, Integer, String, Boolean, desc, asc, func

from sqlalchemy import create_engine
sqlEngine = create_engine('sqlite:///:memory:',echo=False)
from sqlalchemy.orm import sessionmaker, scoped_session
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()

class SimpleTable(Base):
    __tablename__ = 'SimpleTable'

    orderID = Column(Integer, primary_key=True)
    field = Column(Integer)

    def __repr__(self):
    return "<Simple Table: %s>" % self.field

session_factory = sessionmaker(bind=sqlEngine)
Session = scoped_session(session_factory)
Base.metadata.create_all(sqlEngine)

for i in xrange(10):
    var = SimpleTable(field=10)
    Session.add(var)

Session.query(SimpleTable).all()
Session.commit()

from threading import Thread

class TestThread(Thread):

    def run(self):
    print "Running test thread..."
    try:
        print 'Grabbing data from thread:'+str(Session.query(SimpleTable).all())
    except BaseException as e:
        print e
    print "Done running test thread..."

TestThread().start()

print 'Grabbing data outside thread:'+str(Session.query(SimpleTable).all())

以下是带有错误消息的输出:

Running test thread...
(OperationalError) no such table: SimpleTable u'SELECT "SimpleTable"."orderID" AS "SimpleTable_orderID", "SimpleTable".field AS "SimpleTable_field" \nFROM "SimpleTable"' ()
Done running test thread...
Grabbing data outside thread:[<Simple Table: 10>, <Simple Table: 10>, <Simple Table: 10>, <Simple Table: 10>, <Simple Table: 10>, <Simple Table: 10>, <Simple Table: 10>, <Simple Table: 10>, <Simple Table: 10>, <Simple Table: 10>]

该表因某些原因不存在。我已经阅读并重读了文档以及互联网上的多个帖子,并且似乎scoped_session存在于上述的确切情况。有人可以开导我吗?

2 个答案:

答案 0 :(得分:1)

我遇到了同样的情况。我怀疑&#34;在记忆中&#34; db不支持scoped_session。当我切换到基于普通文件的数据库时,问题就消失了。

答案 1 :(得分:0)

只要您手动将会话实例绑定到请求而不是直接线程,您就可以使用 scoped_session 工厂。检查非必需的 scopefunc 参数。它应该需要一些可散列的对象。当使用带有可散列请求的 Web 框架时,您可以将 id 直接作为值。如果不只是将其包装成可散列对象。

from my_web_framework import get_current_request, on_request_end
from sqlalchemy.orm import scoped_session, sessionmaker

Session = scoped_session(sessionmaker(bind=some_engine), scopefunc=get_current_request)

@on_request_end
def remove_session(req):
    Session.remove()

记住会话必须在请求结束时手动销毁!您会在 using-custom-created-scopes

下找到更多信息