Grrr keep only the file write in the GUI thread not the other way around

This commit is contained in:
Kovid Goyal 2010-09-25 23:26:28 -06:00
parent da949796b0
commit 2d406112a4
3 changed files with 40 additions and 28 deletions

View File

@ -157,7 +157,7 @@ class BooksModel(QAbstractTableModel): # {{{
self.cover_cache = CoverCache(db, FunctionDispatcher(self.db.cover)) self.cover_cache = CoverCache(db, FunctionDispatcher(self.db.cover))
self.cover_cache.start() self.cover_cache.start()
self.metadata_backup = MetadataBackup(db, self.metadata_backup = MetadataBackup(db,
FunctionDispatcher(self.db.dump_metadata)) self.db.dump_metadata)
self.metadata_backup.start() self.metadata_backup.start()
def refresh_cover(event, ids): def refresh_cover(event, ids):
if event == 'cover' and self.cover_cache is not None: if event == 'cover' and self.cover_cache is not None:

View File

@ -28,8 +28,9 @@ class MetadataBackup(Thread): # {{{
self.daemon = True self.daemon = True
self.db = db self.db = db
self.dump_func = dump_func self.dump_func = dump_func
self.dump_queue = Queue()
self.keep_running = True self.keep_running = True
from calibre.gui2 import FunctionDispatcher
self.do_write = FunctionDispatcher(self.write)
def stop(self): def stop(self):
self.keep_running = False self.keep_running = False
@ -43,32 +44,43 @@ class MetadataBackup(Thread): # {{{
except: except:
# Happens during interpreter shutdown # Happens during interpreter shutdown
break break
if self.dump_func([id_], dump_queue=self.dump_queue) is None:
# An exception occurred in dump_func, retry once
prints('Failed to get backup metadata for id:', id_, 'once')
time.sleep(2)
if not self.dump_func([id_], dump_queue=self.dump_queue):
prints('Failed to get backup metadata for id:', id_, 'again, giving up')
while True:
try:
path, raw = self.dump_queue.get_nowait()
except:
break
else:
try:
with open(path, 'wb') as f:
f.write(raw)
except:
prints('Failed to write backup metadata for id:', id_, 'once')
time.sleep(2)
try:
with open(path, 'wb') as f:
f.write(raw)
except:
prints('Failed to write backup metadata for id:', id_,
'again, giving up')
time.sleep(0.2) # Limit to five per second dump = []
try:
self.dump_func([id_], dump_queue=dump)
except:
prints('Failed to get backup metadata for id:', id_, 'once')
import traceback
traceback.print_exc()
time.sleep(2)
dump = []
try:
self.dump_func([id_], dump_queue=dump)
except:
prints('Failed to get backup metadata for id:', id_, 'again, giving up')
traceback.print_exc()
continue
try:
path, raw = dump[0]
except:
break
try:
self.do_write(path, raw)
except:
prints('Failed to write backup metadata for id:', id_, 'once')
time.sleep(2)
try:
self.do_write(path, raw)
except:
prints('Failed to write backup metadata for id:', id_,
'again, giving up')
time.sleep(0.5) # Limit to two per second
def write(self, path, raw):
with open(path, 'wb') as f:
f.write(raw)
# }}} # }}}

View File

@ -587,7 +587,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
with open(path, 'wb') as f: with open(path, 'wb') as f:
f.write(raw) f.write(raw)
else: else:
dump_queue.put((path, raw)) dump_queue.append((path, raw))
if remove_from_dirtied: if remove_from_dirtied:
self.conn.execute('DELETE FROM metadata_dirtied WHERE book=?', self.conn.execute('DELETE FROM metadata_dirtied WHERE book=?',
(book_id,)) (book_id,))