我使用 Python 2.7 和 SQLite3 数据库。我想在数据库上运行可能需要一些时间的更新查询。另一方面,我不希望用户必须等待。因此我想启动一个新线程来进行数据库更新。
Python 抛出一个错误。有没有一种有效的方法来告诉数据库在它自己的线程中进行更新而不必等待线程完成?
第 39 行,在执行 ProgrammingError: SQLite objects created in a thread can only be used in the same thread.The object was created in thread id 3648 and this is thread id 6444
就示例而言,我正在尝试编写 Anki 插件。产生该错误的插件代码是:
from anki.sched import Scheduler
import threading
def burying(self, card):
buryingThread = threading.Thread(target = self._burySiblings, args = (card,))
buryingThread.start()
def newGetCard(self):
"Pop the next card from the queue. None if finished."
self._checkDay()
if not self._haveQueues:
self.reset()
card = self._getCard()
if card:
burying(self, card)
self.reps += 1
card.startTimer()
return card
__oldFunc = Scheduler.getCard
Scheduler.getCard = newGetCard