Skip to content

Commit

Permalink
cleanup🧼
Browse files Browse the repository at this point in the history
  • Loading branch information
dimaqq committed Sep 20, 2024
1 parent eb11c00 commit 68d6aae
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 25 deletions.
12 changes: 4 additions & 8 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down
29 changes: 12 additions & 17 deletions juju/_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -34,34 +35,28 @@ 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__()
self._cond = threading.Condition()
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")
Expand Down

0 comments on commit 68d6aae

Please sign in to comment.