From 68d6aae77ab83e9acfc3ff0930ee33c928b6f0b0 Mon Sep 17 00:00:00 2001 From: Dima Tisnek Date: Fri, 20 Sep 2024 22:45:07 +0900 Subject: [PATCH] =?UTF-8?q?cleanup=F0=9F=A7=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example.py | 12 ++++-------- juju/_sync.py | 29 ++++++++++++----------------- 2 files changed, 16 insertions(+), 25 deletions(-) diff --git a/example.py b/example.py index 593f48f3e..2ea2aa548 100644 --- a/example.py +++ b/example.py @@ -8,23 +8,19 @@ async def main() -> None: m = Model() await m.connect(model_name="testm") for app_name, app in m.applications.items(): - print(f"main loop {'-'*40}") - print(m.applications[app_name].name) - - print(f"helper loop {'+'*40}") print(app, app.charm_name) + #print(m.applications[app_name].exposed) #print(m.applications[app_name].charm_url) #print(m.applications[app_name].owner_tag) - # print(m.applications[app_name].life) + #print(m.applications[app_name].life) #print(m.applications[app_name].min_units) #print(m.applications[app_name].constraints) - # print(m.applications[app_name].constraints.arch) + #print(m.applications[app_name].constraints.arch) #print(m.applications[app_name].subordinate) - # print(m.applications[app_name].status) + #print(m.applications[app_name].status) #print(m.applications[app_name].workload_version) - # FIXME why sudden framing/asyncio error on disconnect? await m.disconnect() diff --git a/juju/_sync.py b/juju/_sync.py index 5ab6c1feb..9917a088d 100644 --- a/juju/_sync.py +++ b/juju/_sync.py @@ -13,6 +13,7 @@ class SyncProxy(threading.Thread): + # FIXME condition variable is overused, maybe a simpler primitive would do _cond: threading.Condition _conn: connection.Connection | None _loop: asyncio.AbstractEventLoop | None @@ -21,11 +22,11 @@ class SyncProxy(threading.Thread): def new_running(cls, *, connection_kwargs: dict) -> Self: self = cls(connection_kwargs=connection_kwargs) self.start() + self.setup_connection() return self def call(self, coro: Coroutine[None, None, R]) -> R: assert self._loop - assert self._conn # for sanity, really return asyncio.run_coroutine_threadsafe(coro, self._loop).result() @property @@ -34,19 +35,13 @@ def connection(self) -> connection.Connection: return self._conn def stop(self) -> None: - try: - if self._conn: - # FIXME somehow I'm getting Bad file description here - # and it's deep in the websockets/legacy/framing... - # And I was not getting the same when in prev revision with manual helper... - self.call(self._conn.close()) - self._conn = None - with self._cond: - if self._loop: - self._loop.call_soon_threadsafe(self._loop.stop) - self.join() - except Exception: - logging.exception("XXXXXXXXX") + if self._conn: + self.call(self._conn.close()) + self._conn = None + with self._cond: + if self._loop: + self._loop.call_soon_threadsafe(self._loop.stop) + self.join() def __init__(self, *, connection_kwargs: dict): super().__init__() @@ -54,14 +49,14 @@ def __init__(self, *, connection_kwargs: dict): self._conn = None self._connection_kwargs = connection_kwargs - def start(self): - super().start() - + def setup_connection(self): + # Ensure that .run() registered new event loop in the helper thread with self._cond: while not self._loop: self._cond.wait() try: + # FIXME, is this safe if .run_forever() is late? asyncio.run_coroutine_threadsafe(self._connect(), self._loop).result() except Exception: logging.exception("Helper thread failed to connect")