webapp loop
This commit is contained in:
@@ -121,6 +121,7 @@ def main() -> int:
|
|||||||
#
|
#
|
||||||
# watcher.start()
|
# watcher.start()
|
||||||
# watcher.wait() # blocks until stop() is called via a signal
|
# watcher.wait() # blocks until stop() is called via a signal
|
||||||
|
web_app.wait() # keep process alive while the watcher is disabled
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+19
-5
@@ -265,6 +265,7 @@ class MatchesWebApp:
|
|||||||
self._port = port
|
self._port = port
|
||||||
self._build_lock = threading.Lock()
|
self._build_lock = threading.Lock()
|
||||||
self._app = Flask(__name__)
|
self._app = Flask(__name__)
|
||||||
|
self._thread: "threading.Thread | None" = None
|
||||||
self._register_routes()
|
self._register_routes()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@@ -272,19 +273,32 @@ class MatchesWebApp:
|
|||||||
return self._app
|
return self._app
|
||||||
|
|
||||||
def start(self) -> threading.Thread:
|
def start(self) -> threading.Thread:
|
||||||
"""Starts the Flask server on a daemon thread and returns it."""
|
"""
|
||||||
thread = threading.Thread(
|
Starts the Flask server on a background thread and returns it.
|
||||||
|
|
||||||
|
The thread is non-daemon so the process stays alive even when the
|
||||||
|
caller does not explicitly join() — important when this is the
|
||||||
|
only foreground task (e.g. watcher disabled for testing).
|
||||||
|
"""
|
||||||
|
if self._thread is not None and self._thread.is_alive():
|
||||||
|
return self._thread
|
||||||
|
self._thread = threading.Thread(
|
||||||
target=self._app.run,
|
target=self._app.run,
|
||||||
kwargs={"host": self._host, "port": self._port,
|
kwargs={"host": self._host, "port": self._port,
|
||||||
"debug": False, "use_reloader": False,
|
"debug": False, "use_reloader": False,
|
||||||
"threaded": True},
|
"threaded": True},
|
||||||
name="MatchesWebApp",
|
name="MatchesWebApp",
|
||||||
daemon=True,
|
daemon=False,
|
||||||
)
|
)
|
||||||
thread.start()
|
self._thread.start()
|
||||||
print(f"[MatchesWebApp] listening on {self._host}:{self._port}",
|
print(f"[MatchesWebApp] listening on {self._host}:{self._port}",
|
||||||
flush=True)
|
flush=True)
|
||||||
return thread
|
return self._thread
|
||||||
|
|
||||||
|
def wait(self) -> None:
|
||||||
|
"""Blocks until the Flask thread exits (or returns immediately if not started)."""
|
||||||
|
if self._thread is not None:
|
||||||
|
self._thread.join()
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
# Routes
|
# Routes
|
||||||
|
|||||||
Reference in New Issue
Block a user