SQLite& Sql Alchemy SingletonThreadPool - 我可以共享一个连接对象吗?

时间:2017-01-04 17:07:28

标签: python sqlite sqlalchemy

我收到错误的表单:

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吗?)

1 个答案:

答案 0 :(得分:2)

如果您使用的是sqlite3,则只需传递check_same_thread参数,如下所示:

create_engine(db_path, connect_args={'check_same_thread': False})