From 732ff20cf9d438537129612124a82b7789881c97 Mon Sep 17 00:00:00 2001 From: Joshua Watt Date: Fri, 3 Nov 2023 08:26:21 -0600 Subject: bitbake: asyncrpc: Add context manager API Adds context manager API for the asyncrcp client class which allow writing code that will automatically close the connection like so: with hashserv.create_client(address) as client: ... Rework the bitbake-hashclient tool and PR server to use this new API to fix warnings about unclosed event loops when exiting (Bitbake rev: d01d684a0f6398270fe35ed59b7d28f3fd9b7e41) Signed-off-by: Joshua Watt Signed-off-by: Richard Purdie --- bitbake/lib/bb/asyncrpc/client.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'bitbake/lib/bb') diff --git a/bitbake/lib/bb/asyncrpc/client.py b/bitbake/lib/bb/asyncrpc/client.py index 802c07df1f..009085c306 100644 --- a/bitbake/lib/bb/asyncrpc/client.py +++ b/bitbake/lib/bb/asyncrpc/client.py @@ -103,6 +103,12 @@ class AsyncClient(object): async def ping(self): return await self.invoke({"ping": {}}) + async def __aenter__(self): + return self + + async def __aexit__(self, exc_type, exc_value, traceback): + await self.close() + class Client(object): def __init__(self): @@ -153,3 +159,10 @@ class Client(object): if sys.version_info >= (3, 6): self.loop.run_until_complete(self.loop.shutdown_asyncgens()) self.loop.close() + + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_value, traceback): + self.close() + return False -- cgit v1.2.3-54-g00ecf