SQLite和Sql Alchemy SingletonThreadPool

编程入门 行业动态 更新时间:2024-10-17 19:24:46
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 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)

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})

更多推荐

本文发布于:2023-08-06 22:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1456717.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Sql   SQLite   SingletonThreadPool   Alchemy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!