diff options
author | Joshua Watt <JPEWhacker@gmail.com> | 2023-11-03 08:26:21 -0600 |
---|---|---|
committer | Steve Sakoman <steve@sakoman.com> | 2024-01-05 06:59:11 -1000 |
commit | ee090484cc25d760b8c20f18add17b5eff485b40 (patch) | |
tree | 4490fbfb6995b54d52b9fb1205458b035c1bdd74 /lib | |
parent | 2c6183594279e2e9d03f11155ad969448869c863 (diff) | |
download | bitbake-ee090484cc25d760b8c20f18add17b5eff485b40.tar.gz |
asyncrpc: Add context manager APIyocto-4.0.162022-04.16-kirkstone2.0.16
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
Signed-off-by: Joshua Watt <JPEWhacker@gmail.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
(cherry picked from commit d01d684a0f6398270fe35ed59b7d28f3fd9b7e41)
Signed-off-by: Steve Sakoman <steve@sakoman.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/bb/asyncrpc/client.py | 13 | ||||
-rw-r--r-- | lib/prserv/serv.py | 6 |
2 files changed, 16 insertions, 3 deletions
diff --git a/lib/bb/asyncrpc/client.py b/lib/bb/asyncrpc/client.py index fa042bbe8..dcbe7e576 100644 --- a/lib/bb/asyncrpc/client.py +++ b/lib/bb/asyncrpc/client.py @@ -126,6 +126,12 @@ class AsyncClient(object): {'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): @@ -176,3 +182,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/lib/prserv/serv.py b/lib/prserv/serv.py index c686b2065..0db6ebc70 100644 --- a/lib/prserv/serv.py +++ b/lib/prserv/serv.py @@ -344,9 +344,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 |