无fles的SQLAlchemy多线程

2024-04-24 19:41:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从主线程和后台线程访问sqlite数据库文件。问题是,无论我如何更改代码,我总是会遇到以下问题:

ProgrammingError: SQLite objects created in a thread can only be used in that same thread.The object was created in thread id -1250925472 and this is thread id -1225814016

我的代码是这样的:

engine = create_engine('sqlite:///data/storage.sqlite', poolclass=NullPool)
Footprint.Base.metadata.create_all(engine)
session_factory = sessionmaker(bind=engine)
Session = scoped_session(session_factory)

def storeData(fp):
    s = Session()
    s.add(fp)
    s.commit()

有人知道怎么解决这个恼人的问题吗?你知道吗


Tags: 代码inidsqlitesessionfactorycreate线程