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 | |
| 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')
| -rwxr-xr-x | bitbake/bin/bitbake-hashclient | 36 | ||||
| -rw-r--r-- | bitbake/lib/bb/asyncrpc/client.py | 13 | ||||
| -rw-r--r-- | bitbake/lib/prserv/serv.py | 6 |
3 files changed, 33 insertions, 22 deletions
diff --git a/bitbake/bin/bitbake-hashclient b/bitbake/bin/bitbake-hashclient index 3f265e8fa7..a02a65b937 100755 --- a/bitbake/bin/bitbake-hashclient +++ b/bitbake/bin/bitbake-hashclient | |||
| @@ -56,25 +56,24 @@ def main(): | |||
| 56 | nonlocal missed_hashes | 56 | nonlocal missed_hashes |
| 57 | nonlocal max_time | 57 | nonlocal max_time |
| 58 | 58 | ||
| 59 | client = hashserv.create_client(args.address) | 59 | with hashserv.create_client(args.address) as client: |
| 60 | 60 | for i in range(args.requests): | |
| 61 | for i in range(args.requests): | 61 | taskhash = hashlib.sha256() |
| 62 | taskhash = hashlib.sha256() | 62 | taskhash.update(args.taskhash_seed.encode('utf-8')) |
| 63 | taskhash.update(args.taskhash_seed.encode('utf-8')) | 63 | taskhash.update(str(i).encode('utf-8')) |
| 64 | taskhash.update(str(i).encode('utf-8')) | ||
| 65 | 64 | ||
| 66 | start_time = time.perf_counter() | 65 | start_time = time.perf_counter() |
| 67 | l = client.get_unihash(METHOD, taskhash.hexdigest()) | 66 | l = client.get_unihash(METHOD, taskhash.hexdigest()) |
| 68 | elapsed = time.perf_counter() - start_time | 67 | elapsed = time.perf_counter() - start_time |
| 69 | 68 | ||
| 70 | with lock: | 69 | with lock: |
| 71 | if l: | 70 | if l: |
| 72 | found_hashes += 1 | 71 | found_hashes += 1 |
| 73 | else: | 72 | else: |
| 74 | missed_hashes += 1 | 73 | missed_hashes += 1 |
| 75 | 74 | ||
| 76 | max_time = max(elapsed, max_time) | 75 | max_time = max(elapsed, max_time) |
| 77 | pbar.update() | 76 | pbar.update() |
| 78 | 77 | ||
| 79 | max_time = 0 | 78 | max_time = 0 |
| 80 | found_hashes = 0 | 79 | found_hashes = 0 |
| @@ -174,9 +173,8 @@ def main(): | |||
| 174 | 173 | ||
| 175 | func = getattr(args, 'func', None) | 174 | func = getattr(args, 'func', None) |
| 176 | if func: | 175 | if func: |
| 177 | client = hashserv.create_client(args.address) | 176 | with hashserv.create_client(args.address) as client: |
| 178 | 177 | return func(args, client) | |
| 179 | return func(args, client) | ||
| 180 | 178 | ||
| 181 | return 0 | 179 | return 0 |
| 182 | 180 | ||
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 |
