From 4cc3bb9398625bf417b2478d1948e60fb0408878 Mon Sep 17 00:00:00 2001 From: Alex Burke Date: Wed, 28 Aug 2024 13:37:30 +0200 Subject: [PATCH] stop extending thread it's just too difficult to get right when also intending to keep a reference to an event that must be consulted from the parent - opt insteda to create an objectthat quacks much like a Thread but creates one internally at a time of its pleasing --- tests/support/serversupp.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/tests/support/serversupp.py b/tests/support/serversupp.py index efbce831c..b14b4cb49 100644 --- a/tests/support/serversupp.py +++ b/tests/support/serversupp.py @@ -26,10 +26,10 @@ """Configuration related details within the test support library.""" -import threading +from threading import Thread, Event as ThreadEvent -class ServerWithinThreadExecutor(threading.Thread): +class ServerWithinThreadExecutor: """Execute a server within a thread ensuring we are able to block until it is ready to recieve test requests. @@ -38,10 +38,10 @@ class ServerWithinThreadExecutor(threading.Thread): the server is ready to handle requests.""" def __init__(self, ServerClass, *args, **kwargs): - super(ServerWithinThreadExecutor, self).__init__() self._serverclass = ServerClass self._arguments = (args, kwargs) - self._started = threading.Event() + self._started = ThreadEvent() + self._thread = None self._wrapped = None def run(self): @@ -56,15 +56,20 @@ def run(self): except Exception as e: pass + def start(self): + self._thread = Thread(target=self.run) + self._thread.start() + def start_wait_until_ready(self): self.start() - if not self._started.is_set(): - self._started.wait() + self._started.wait() return self def stop(self): self.stop_server() - self.join() + self._wrapped = None + self._thread.join() + self._thread = None def stop_server(self): self._wrapped.shutdown()