【问题标题】:Python complains SQLite objects created in a thread can only be used in that one, but I'm not trying to access any SQLite object in any other threadPython 抱怨在一个线程中创建的 SQLite 对象只能在那个线程中使用,但我没有尝试访问任何其他线程中的任何 SQLite 对象
【发布时间】:2021-06-10 02:49:45
【问题描述】:

导入的文件中包含在导入时运行的代码。

engine = create_engine("sqlite:///database.db")
Base.metadata.create_all(engine)

在一个不相关的文件(但仍然是同一个包的一部分)中,我使用了这段代码。

t = threading.Thread(target=_play)
t.start()

这是我唯一创建的另一个线程,_play 只是播放一个声音文件。它没有对 SQLite 的任何引用。

我收到的错误如下。

sqlite3.ProgrammingError: SQLite objects created in a thread can only be used in that same thread.

为什么我会收到这样的错误?

【问题讨论】:

  • 请创建一个演示问题的最小工作示例!

标签: python multithreading sqlite


【解决方案1】:

我仍然不确定为什么会发生这样的事情,但playsound 模块是这里的问题。我切换到simpleaudio 并没有这个问题了。

【讨论】:

  • 如果没有可重复的问题和解释,此答案将无济于事。
猜你喜欢
  • 2018-06-21
  • 2017-03-01
  • 2020-09-12
  • 2019-10-30
  • 2021-09-20
  • 2019-10-21
  • 2021-01-16
  • 2016-03-04
  • 2012-10-29
相关资源
最近更新 更多