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 +++++++++++++ bitbake/lib/prserv/serv.py | 6 +++--- 2 files changed, 16 insertions(+), 3 deletions(-) (limited to 'bitbake/lib') 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 diff --git a/bitbake/lib/prserv/serv.py b/bitbake/lib/prserv/serv.py index ea7933164b..6168eb183d 100644 --- a/bitbake/lib/prserv/serv.py +++ b/bitbake/lib/prserv/serv.py @@ -345,9 +345,9 @@ def auto_shutdown(): def ping(host, port): from . import client - conn = client.PRClient() - conn.connect_tcp(host, port) - return conn.ping() + with client.PRClient() as conn: + conn.connect_tcp(host, port) + return conn.ping() def connect(host, port): from . import client -- cgit v1.2.3-54-g00ecf