Skip to content

Commit

Permalink
Refactor data update threads
Browse files Browse the repository at this point in the history
  • Loading branch information
timmo001 committed Dec 30, 2023
1 parent 713bbf5 commit 9b2b3e0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
18 changes: 9 additions & 9 deletions systembridgebackend/handlers/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def __init__(
"""Initialise."""
super().__init__()
self._updated_callback = updated_callback
self._update_thread: DataUpdateThread | None = None
self._update_media_thread: MediaUpdateThread | None = None
self.update_data_thread: DataUpdateThread | None = None
self.update_media_thread: MediaUpdateThread | None = None

self.data = ModulesData()

Expand All @@ -35,23 +35,23 @@ async def _data_updated_callback(

def request_update_data(self) -> None:
"""Request update data."""
if self._update_thread is not None and self._update_thread.is_alive():
if self.update_data_thread is not None and self.update_data_thread.is_alive():
self._logger.info("Update data thread already running")
return

self._logger.info("Starting update data thread..")
self._update_thread = DataUpdateThread(self._data_updated_callback)
self._update_thread.start()
self.update_data_thread = DataUpdateThread(self._data_updated_callback)
self.update_data_thread.start()

def request_update_media_data(self) -> None:
"""Request update media data."""
if (
self._update_media_thread is not None
and self._update_media_thread.is_alive()
self.update_media_thread is not None
and self.update_media_thread.is_alive()
):
self._logger.info("Update media thread already running")
return

self._logger.info("Starting update media thread..")
self._update_media_thread = MediaUpdateThread(self._data_updated_callback)
self._update_media_thread.start()
self.update_media_thread = MediaUpdateThread(self._data_updated_callback)
self.update_media_thread.start()
2 changes: 1 addition & 1 deletion systembridgebackend/modules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,4 @@ async def update_data(self) -> None:
# Stagger the updates to avoid overloading the system
await asyncio.sleep(1)

self._logger.info("Data updates requested")
self._logger.info("Data update threads started")
8 changes: 4 additions & 4 deletions systembridgebackend/server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,10 @@ def exit_application(self) -> None:
self._logger.info("Event loop stopped")

# Stop threads
if api_app.data_update._update_thread is not None:
api_app.data_update._update_thread.join(timeout=2)
if api_app.data_update._update_media_thread is not None:
api_app.data_update._update_media_thread.join(timeout=2)
if api_app.data_update.update_data_thread is not None:
api_app.data_update.update_data_thread.join(timeout=2)
if api_app.data_update.update_media_thread is not None:
api_app.data_update.update_media_thread.join(timeout=2)
self._logger.info("Threads joined")

self._logger.info("Exit Application")
Expand Down

0 comments on commit 9b2b3e0

Please sign in to comment.