2013-11-09 125 views
0

我使用Python 2.7和SQLite3數據庫。我想在數據庫上運行需要一些時間的更新查詢。另一方面,我不希望用戶必須等待。 因此,我想開始一個新的線程來做數據庫更新。多線程和Python SQL3

Python引發錯誤。有沒有一種有效的方式來告訴數據庫在它自己的線程中執行更新,而無需等待線程完成?

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

至於示例去我試圖寫一個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 
+0

這將是有幫助的獨立的例子。 –

回答

0

退房芹菜項目,如果你使用Django的它會更加直截了當www.celeryproject.org