【问题标题】:Multiple Threads and Python SQL3多线程和 Python SQL3
【发布时间】:2013-11-09 20:49:39
【问题描述】:

我使用 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

【问题讨论】:

  • 有一个独立的例子会很有帮助。

标签: python multithreading sqlite python-2.7


【解决方案1】:

查看 celery 项目,如果您使用的是 django,它会更加直接 www.celeryproject.org

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多