Sqlite和Sql Alchemy SingletonThreadPool - 我可以共享一个连接对象吗?(SQLite & Sql Alchemy SingletonThreadPool - can I share a connection object?)
我收到了错误的表格:
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吗?)
I'm getting errors of the form:
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 139662493492992in 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)I had expected the SingletonThreadPool to solve this problem. What am I missing?
(bonus question: for the sake of reduced headache, should I move to MySQL?)
最满意答案
如果您使用的是sqlite3,则只需传递check_same_thread参数,如下所示:
create_engine(db_path, connect_args={'check_same_thread': False})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})更多推荐
发布评论