From 2d406112a477d3b65e014e548252fefdaa806de5 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 25 Sep 2010 23:26:28 -0600 Subject: [PATCH] Grrr keep only the file write in the GUI thread not the other way around --- src/calibre/gui2/library/models.py | 2 +- src/calibre/library/caches.py | 64 ++++++++++++++++++------------ src/calibre/library/database2.py | 2 +- 3 files changed, 40 insertions(+), 28 deletions(-) diff --git a/src/calibre/gui2/library/models.py b/src/calibre/gui2/library/models.py index fe1701a918..01f597caf4 100644 --- a/src/calibre/gui2/library/models.py +++ b/src/calibre/gui2/library/models.py @@ -157,7 +157,7 @@ class BooksModel(QAbstractTableModel): # {{{ self.cover_cache = CoverCache(db, FunctionDispatcher(self.db.cover)) self.cover_cache.start() self.metadata_backup = MetadataBackup(db, - FunctionDispatcher(self.db.dump_metadata)) + self.db.dump_metadata) self.metadata_backup.start() def refresh_cover(event, ids): if event == 'cover' and self.cover_cache is not None: diff --git a/src/calibre/library/caches.py b/src/calibre/library/caches.py index 8261ca40fb..e675c97c76 100644 --- a/src/calibre/library/caches.py +++ b/src/calibre/library/caches.py @@ -28,8 +28,9 @@ class MetadataBackup(Thread): # {{{ self.daemon = True self.db = db self.dump_func = dump_func - self.dump_queue = Queue() self.keep_running = True + from calibre.gui2 import FunctionDispatcher + self.do_write = FunctionDispatcher(self.write) def stop(self): self.keep_running = False @@ -43,32 +44,43 @@ class MetadataBackup(Thread): # {{{ except: # Happens during interpreter shutdown 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) + # }}} diff --git a/src/calibre/library/database2.py b/src/calibre/library/database2.py index 61c52cf6b7..8c34510de4 100644 --- a/src/calibre/library/database2.py +++ b/src/calibre/library/database2.py @@ -587,7 +587,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): with open(path, 'wb') as f: f.write(raw) else: - dump_queue.put((path, raw)) + dump_queue.append((path, raw)) if remove_from_dirtied: self.conn.execute('DELETE FROM metadata_dirtied WHERE book=?', (book_id,))