From 7121f2d6ed6efd5d8da924fcce3d9235ecc8657e Mon Sep 17 00:00:00 2001 From: Alessandro Felder Date: Wed, 23 Aug 2023 14:51:50 +0100 Subject: [PATCH] offload atlas downloading to separate thread (#63) --- brainrender_napari/widgets/atlas_table_view.py | 10 +++++++++- tests/test_unit/test_atlas_table_view.py | 3 ++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/brainrender_napari/widgets/atlas_table_view.py b/brainrender_napari/widgets/atlas_table_view.py index 46ad80a..2507139 100644 --- a/brainrender_napari/widgets/atlas_table_view.py +++ b/brainrender_napari/widgets/atlas_table_view.py @@ -12,6 +12,7 @@ get_downloaded_atlases, ) from bg_atlasapi.update_atlases import install_atlas +from napari.qt import thread_worker from qtpy.QtCore import QAbstractTableModel, QModelIndex, Qt, Signal from qtpy.QtWidgets import QMenu, QTableView, QWidget @@ -157,8 +158,15 @@ def _on_row_double_clicked(self): def _on_download_atlas_confirmed(self): """Downloads an atlas and signals that this has happened.""" atlas_name = self.selected_atlas_name() + worker = self._install_atlas_in_thread(atlas_name) + worker.returned.connect(self.download_atlas_confirmed.emit) + worker.start() + + @thread_worker + def _install_atlas_in_thread(self, atlas_name: str): + """Installs the currently selected atlas in a separate thread.""" install_atlas(atlas_name) - self.download_atlas_confirmed.emit(atlas_name) + return atlas_name def _on_current_changed(self): """Emits a signal with the newly selected atlas name""" diff --git a/tests/test_unit/test_atlas_table_view.py b/tests/test_unit/test_atlas_table_view.py index 6b61109..a5ff098 100644 --- a/tests/test_unit/test_atlas_table_view.py +++ b/tests/test_unit/test_atlas_table_view.py @@ -199,7 +199,8 @@ def test_download_confirmed_callback(atlas_table_view, qtbot): ) # sanity check that local copy is gone with qtbot.waitSignal( - atlas_table_view.download_atlas_confirmed + atlas_table_view.download_atlas_confirmed, + timeout=150000, # assumes atlas can be installed in 2.5 minutes! ) as download_atlas_confirmed_signal: model_index = atlas_table_view.model().index(0, 0) atlas_table_view.setCurrentIndex(model_index)