From e67d2bf2551b9d5999c04e5eb0eb34d759d0d9a8 Mon Sep 17 00:00:00 2001
From: David Trattnig <david.trattnig@o94.at>
Date: Wed, 16 Dec 2020 09:37:08 +0100
Subject: [PATCH] Parent:child dependency using join(). #62

---
 src/core/control.py         | 11 ++++++++---
 src/scheduling/scheduler.py |  6 +++---
 2 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/src/core/control.py b/src/core/control.py
index 9adac8cc..516fdc39 100644
--- a/src/core/control.py
+++ b/src/core/control.py
@@ -248,14 +248,19 @@ class EngineExecutor(Timer):
                 self.start()
 
 
+    def on_ready(self, func):
+        """
+        Calls the passed function `func` when the timer is ready.
+        """
+        self.join()
+        func()
+
+
     def wait_for_parent(self):
         """
         Child timers are dependend on their parents. So let's wait until parents are done with their stuff.
         """
         if self.parent_timer:
-            # Wait a bit to allow any parent to complete initialization, in case child & parent are instantiated at the 'same' time
-            # Required to avoid "Thread.__init__() not called" exceptions on the parent
-            time.sleep(0.1)
             while self.parent_timer.is_alive():
                 self.logger.info(f"Timer '{self.timer_id}' is waiting for parent timer '{self.parent_timer.timer_id}' to finish")
                 time.sleep(0.2)
diff --git a/src/scheduling/scheduler.py b/src/scheduling/scheduler.py
index 5fb0d445..6e81b39e 100644
--- a/src/scheduling/scheduler.py
+++ b/src/scheduling/scheduler.py
@@ -471,9 +471,9 @@ class PlayCommand(EngineExecutor):
 
         start_preload = entries[0].start_unix - self.config.get("preload_offset")
         start_play = entries[0].start_unix
-        super().__init__("PLAY", None, start_preload, self.do_preload, entries)
-        EngineExecutor("PLAY", self, start_play, self.do_play, entries)
-
+        preload_timer = super().__init__("PLAY", None, start_preload, self.do_preload, entries)
+        self.on_ready(lambda: EngineExecutor("PLAY", self, start_play, self.do_play, entries))
+        
 
     def do_preload(self, entries):
         """
-- 
GitLab