简体   繁体   English

多线程和Python SQL3

[英]Multiple Threads and Python SQL3

I use Python 2.7 and a SQLite3 database. 我使用Python 2.7和SQLite3数据库。 I want to run update queries on the database that can take some time. 我想在数据库上运行更新查询,这可能需要一些时间。 On the other hand I don't want that the user has to wait. 另一方面,我不希望用户必须等待。 Therefore I want to start a new thread to do the database updating. 因此,我想启动一个新线程来进行数据库更新。

Python throws an error. Python引发错误。 Is there an effective way to tell the database to do the update in it's own thread without having to wait for the thread to finish? 有没有一种有效的方法可以告诉数据库在其自己的线程中执行更新,而不必等待线程完成?

line 39, in execute ProgrammingError: SQLite objects created in a thread can only be used in that same thread.The object was created in thread id 3648 and this is thread id 6444 第39行,在execute ProgrammingError中:在线程中创建的SQLite对象只能在同一线程中使用。该对象是在线程ID 3648中创建的,这是线程ID 6444

As far as examples go I'm trying to write an Anki addon. 就示例而言,我正在尝试编写Anki插件。 The Addon code that produces that error is: 产生该错误的插件代码是:

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