Skip to content

Commit

Permalink
use a single thread instead of a nested one
Browse files Browse the repository at this point in the history
  • Loading branch information
shapiromatron committed Jul 9, 2024
1 parent 753b332 commit 99163e7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
11 changes: 2 additions & 9 deletions bmds_ui/desktop/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,10 @@ def setup_django_environment(db: Database):
django.setup()


def _create_django_db(db):
"""Create a django database and sync persistent state with application."""
def create_django_db(db: Database):
log.info(f"Creating {db}")
setup_django_environment(db)
sync_persistent_data()


def create_django_db(config: DesktopConfig, db: Database):
log.info(f"Creating {db}")
thread = Thread(target=_create_django_db, args=(db,), daemon=True)
thread.start()
thread.join()
log.info(f"Creation successful {db}")


Expand Down
19 changes: 9 additions & 10 deletions bmds_ui/desktop/components/database_form.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from asyncio import sleep
from pathlib import Path
from typing import Any

Expand Down Expand Up @@ -158,18 +157,15 @@ async def on_db_create(self) -> None:
db = Database(name=name, description=description, path=db_path)
self._create_django_db(config, db)

@work(exclusive=True)
async def _create_django_db(self, config, db):
@work(exclusive=True, thread=True)
def _create_django_db(self, config, db):
# sleeps are required for loading indicator to show/hide properly
actions = self.get_widget_by_id("actions-row")
actions.loading = True
await sleep(0.1)
self.app.call_from_thread(self.set_loading, True)
config.add_db(db)
Config.sync()
create_django_db(config, db)
actions.loading = False
await sleep(0.1)
self.dismiss(True)
create_django_db(db)
self.app.call_from_thread(self.set_loading, False)
self.app.call_from_thread(self.dismiss, True)

@on(Button.Pressed, "#db-update")
async def on_db_update(self) -> None:
Expand Down Expand Up @@ -205,3 +201,6 @@ async def on_db_delete(self) -> None:
@on(Button.Pressed, "#db-edit-cancel")
def on_db_create_cancel(self) -> None:
self.dismiss(False)

def set_loading(self, status: bool):
self.get_widget_by_id("actions-row").loading = status

0 comments on commit 99163e7

Please sign in to comment.