Skip to content

Commit

Permalink
py: add close()
Browse files Browse the repository at this point in the history
  • Loading branch information
jordens committed Nov 18, 2024
1 parent 6ca797e commit 4267b4f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions py/miniconf-mqtt/miniconf/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ async def run():
assert path.startswith("/") or not path
value = await interface.get(path)
print(f"{path}={value}")
await interface.close()

asyncio.run(run())

Expand Down
11 changes: 11 additions & 0 deletions py/miniconf-mqtt/miniconf/miniconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ def __init__(self, client: Client, prefix: str):
self.listener = asyncio.create_task(self._listen())
self.subscribed = asyncio.Event()

async def close(self):
self.listener.cancel()
for fut in self._inflight.values():
fut.cancel()
try:
await self.listener
except asyncio.CancelledError:
pass
if len(self._inflight) > 0:
await asyncio.wait(self._inflight.values())

async def _listen(self):
await self.client.subscribe(self.response_topic)
LOGGER.info(f"Subscribed to {self.response_topic}")
Expand Down

0 comments on commit 4267b4f

Please sign in to comment.