Stop metadata backup thread before bulk metadata edits to improve performance

This commit is contained in:
Kovid Goyal 2010-09-28 16:55:22 -06:00
parent d45ee9ed9b
commit 96bc9f6bec
4 changed files with 34 additions and 20 deletions

View File

@ -184,7 +184,7 @@ class EditMetadataAction(InterfaceAction):
self.gui.tags_view.blockSignals(True) self.gui.tags_view.blockSignals(True)
try: try:
changed = MetadataBulkDialog(self.gui, rows, changed = MetadataBulkDialog(self.gui, rows,
self.gui.library_view.model().db).changed self.gui.library_view.model()).changed
finally: finally:
self.gui.tags_view.blockSignals(False) self.gui.tags_view.blockSignals(False)
if changed: if changed:

View File

@ -142,12 +142,13 @@ class MetadataBulkDialog(QDialog, Ui_MetadataBulkDialog):
_('Append to field'), _('Append to field'),
] ]
def __init__(self, window, rows, db): def __init__(self, window, rows, model):
QDialog.__init__(self, window) QDialog.__init__(self, window)
Ui_MetadataBulkDialog.__init__(self) Ui_MetadataBulkDialog.__init__(self)
self.setupUi(self) self.setupUi(self)
self.db = db self.model = model
self.ids = [db.id(r) for r in rows] self.db = model.db
self.ids = [self.db.id(r) for r in rows]
self.box_title.setText('<p>' + self.box_title.setText('<p>' +
_('Editing meta information for <b>%d books</b>') % _('Editing meta information for <b>%d books</b>') %
len(rows)) len(rows))
@ -170,7 +171,7 @@ class MetadataBulkDialog(QDialog, Ui_MetadataBulkDialog):
self.tag_editor_button.clicked.connect(self.tag_editor) self.tag_editor_button.clicked.connect(self.tag_editor)
self.autonumber_series.stateChanged[int].connect(self.auto_number_changed) self.autonumber_series.stateChanged[int].connect(self.auto_number_changed)
if len(db.custom_field_keys(include_composites=False)) == 0: if len(self.db.custom_field_keys(include_composites=False)) == 0:
self.central_widget.removeTab(1) self.central_widget.removeTab(1)
else: else:
self.create_custom_column_editors() self.create_custom_column_editors()
@ -617,8 +618,15 @@ class MetadataBulkDialog(QDialog, Ui_MetadataBulkDialog):
self.worker = Worker(args, self.db, self.ids, self.worker = Worker(args, self.db, self.ids,
getattr(self, 'custom_column_widgets', []), getattr(self, 'custom_column_widgets', []),
Dispatcher(bb.accept, parent=bb)) Dispatcher(bb.accept, parent=bb))
self.worker.start()
bb.exec_() # The metadata backup thread causes database commits
# which can slow down bulk editing of large numbers of books
self.model.stop_metadata_backup()
try:
self.worker.start()
bb.exec_()
finally:
self.model.start_metadata_backup()
if self.worker.error is not None: if self.worker.error is not None:
return error_dialog(self, _('Failed'), return error_dialog(self, _('Failed'),

View File

@ -159,17 +159,24 @@ class BooksModel(QAbstractTableModel): # {{{
# do something on the GUI thread. Deadlock. # do something on the GUI thread. Deadlock.
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()
if self.metadata_backup is not None: self.stop_metadata_backup()
self.metadata_backup.stop() self.start_metadata_backup()
# Would like to to a join here, but the thread might be waiting to
# do something on the GUI thread. Deadlock.
self.metadata_backup = MetadataBackup(db)
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:
self.cover_cache.refresh(ids) self.cover_cache.refresh(ids)
db.add_listener(refresh_cover) db.add_listener(refresh_cover)
def start_metadata_backup(self):
self.metadata_backup = MetadataBackup(self.db)
self.metadata_backup.start()
def stop_metadata_backup(self):
if getattr(self, 'metadata_backup', None) is not None:
self.metadata_backup.stop()
# Would like to to a join here, but the thread might be waiting to
# do something on the GUI thread. Deadlock.
def refresh_ids(self, ids, current_row=-1): def refresh_ids(self, ids, current_row=-1):
rows = self.db.refresh_ids(ids) rows = self.db.refresh_ids(ids)
if rows: if rows:

View File

@ -106,14 +106,13 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
d.exec_() d.exec_()
def compact(self, *args): def compact(self, *args):
from calibre.library.caches import MetadataBackup
m = self.gui.library_view.model() m = self.gui.library_view.model()
if m.metadata_backup is not None: m.stop_metadata_backup()
m.metadata_backup.stop() try:
d = CheckIntegrity(m.db, self) d = CheckIntegrity(m.db, self)
d.exec_() d.exec_()
m.metadata_backup = MetadataBackup(m.db) finally:
m.metadata_backup.start() m.start_metadata_backup()
def open_config_dir(self, *args): def open_config_dir(self, *args):
from calibre.utils.config import config_dir from calibre.utils.config import config_dir