繁体   English   中英

多线程和Python SQL3

[英]Multiple Threads and Python SQL3

我使用Python 2.7和SQLite3数据库。 我想在数据库上运行更新查询,这可能需要一些时间。 另一方面,我不希望用户必须等待。 因此,我想启动一个新线程来进行数据库更新。

Python引发错误。 有没有一种有效的方法可以告诉数据库在其自己的线程中执行更新,而不必等待线程完成?

第39行,在execute ProgrammingError中:在线程中创建的SQLite对象只能在同一线程中使用。该对象是在线程ID 3648中创建的,这是线程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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM