简体   繁体   中英

Multiple Threads and Python SQL3

I use Python 2.7 and a SQLite3 database. 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. 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

As far as examples go I'm trying to write an Anki addon. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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