本文介绍了SQLite &Sql Alchemy SingletonThreadPool - 我可以共享连接对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我收到表单错误:
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
在我的多线程应用程序中.
in my multithreaded application.
我正在实例化我的引擎:
I'm instantiating my engine with:
from sqlalchemy.pool import SingletonThreadPool
db_path = "sqlite:///" + cwd + "/data/data.db"
create_engine(db_path, poolclass=SingletonThreadPool, pool_size=50)
我原以为 SingletonThreadPool
可以解决这个问题.我错过了什么?
I had expected the SingletonThreadPool
to solve this problem. What am I missing?
(额外问题:为了减少头痛,我应该改用 MySQL 吗?)
(bonus question: for the sake of reduced headache, should I move to MySQL?)
推荐答案
如果你使用 sqlite3 那么你只需要传递如下 check_same_thread
参数:
If you are using sqlite3 then you just have to pass the check_same_thread
parameter as below:
create_engine(db_path, connect_args={'check_same_thread': False})
这篇关于SQLite &Sql Alchemy SingletonThreadPool - 我可以共享连接对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!