diff options
| author | Joshua Watt <JPEWhacker@gmail.com> | 2023-11-03 08:26:21 -0600 |
|---|---|---|
| committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2023-11-09 17:33:02 +0000 |
| commit | 732ff20cf9d438537129612124a82b7789881c97 (patch) | |
| tree | 157dfc1450df3f29e0d90fcdeeb16614d9cdf957 /bitbake/lib | |
| parent | 2484bd893189bc24ac210edf2de3b7e1115eebd3 (diff) | |
| download | poky-732ff20cf9d438537129612124a82b7789881c97.tar.gz | |
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 <JPEWhacker@gmail.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'bitbake/lib')
| -rw-r--r-- | bitbake/lib/bb/asyncrpc/client.py | 13 | ||||
| -rw-r--r-- | bitbake/lib/prserv/serv.py | 6 |
2 files changed, 16 insertions, 3 deletions
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): | |||
| 103 | async def ping(self): | 103 | async def ping(self): |
| 104 | return await self.invoke({"ping": {}}) | 104 | return await self.invoke({"ping": {}}) |
| 105 | 105 | ||
| 106 | async def __aenter__(self): | ||
| 107 | return self | ||
| 108 | |||
| 109 | async def __aexit__(self, exc_type, exc_value, traceback): | ||
| 110 | await self.close() | ||
| 111 | |||
| 106 | 112 | ||
| 107 | class Client(object): | 113 | class Client(object): |
| 108 | def __init__(self): | 114 | def __init__(self): |
| @@ -153,3 +159,10 @@ class Client(object): | |||
| 153 | if sys.version_info >= (3, 6): | 159 | if sys.version_info >= (3, 6): |
| 154 | self.loop.run_until_complete(self.loop.shutdown_asyncgens()) | 160 | self.loop.run_until_complete(self.loop.shutdown_asyncgens()) |
| 155 | self.loop.close() | 161 | self.loop.close() |
| 162 | |||
| 163 | def __enter__(self): | ||
| 164 | return self | ||
| 165 | |||
| 166 | def __exit__(self, exc_type, exc_value, traceback): | ||
| 167 | self.close() | ||
| 168 | 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(): | |||
| 345 | def ping(host, port): | 345 | def ping(host, port): |
| 346 | from . import client | 346 | from . import client |
| 347 | 347 | ||
| 348 | conn = client.PRClient() | 348 | with client.PRClient() as conn: |
| 349 | conn.connect_tcp(host, port) | 349 | conn.connect_tcp(host, port) |
| 350 | return conn.ping() | 350 | return conn.ping() |
| 351 | 351 | ||
| 352 | def connect(host, port): | 352 | def connect(host, port): |
| 353 | from . import client | 353 | from . import client |
