我收到错误的表单:
sqlite3.ProgrammingError: SQLite objects created in a thread can only be used in that same thread.The object was created in thread id 139661426296576 and this is thread id 139662493492992
在我的多线程应用程序中。
我用:
实例化我的引擎from sqlalchemy.pool import SingletonThreadPool
db_path = "sqlite:///" + cwd + "/data/data.db"
create_engine(db_path, poolclass=SingletonThreadPool, pool_size=50)
我原以为SingletonThreadPool
可以解决这个问题。我错过了什么?
(奖金问题:为了减少头痛,我应该转向MySQL吗?)
答案 0 :(得分:2)
如果您使用的是sqlite3,则只需传递check_same_thread
参数,如下所示:
create_engine(db_path, connect_args={'check_same_thread': False})