diff options
| author | Joshua Watt <JPEWhacker@gmail.com> | 2023-11-03 08:26:21 -0600 |
|---|---|---|
| committer | Steve Sakoman <steve@sakoman.com> | 2024-01-10 05:02:09 -1000 |
| commit | afe1706a7795dd07e7b92e85019c17acedb4ac85 (patch) | |
| tree | 36aee0d78333c6393d672c957f5abf8cdbbdb865 | |
| parent | f768ffb8916feb6542fcbe3e946cbf30e247b151 (diff) | |
| download | poky-afe1706a7795dd07e7b92e85019c17acedb4ac85.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: 52226a7244968c8dad6f4ee9ccec57ac1979217a)
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>
| -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 fa042bbe87..dcbe7e5762 100644 --- a/bitbake/lib/bb/asyncrpc/client.py +++ b/bitbake/lib/bb/asyncrpc/client.py | |||
| @@ -126,6 +126,12 @@ class AsyncClient(object): | |||
| 126 | {'ping': {}} | 126 | {'ping': {}} |
| 127 | ) | 127 | ) |
| 128 | 128 | ||
| 129 | async def __aenter__(self): | ||
| 130 | return self | ||
| 131 | |||
| 132 | async def __aexit__(self, exc_type, exc_value, traceback): | ||
| 133 | await self.close() | ||
| 134 | |||
| 129 | 135 | ||
| 130 | class Client(object): | 136 | class Client(object): |
| 131 | def __init__(self): | 137 | def __init__(self): |
| @@ -176,3 +182,10 @@ class Client(object): | |||
| 176 | if sys.version_info >= (3, 6): | 182 | if sys.version_info >= (3, 6): |
| 177 | self.loop.run_until_complete(self.loop.shutdown_asyncgens()) | 183 | self.loop.run_until_complete(self.loop.shutdown_asyncgens()) |
| 178 | self.loop.close() | 184 | self.loop.close() |
| 185 | |||
| 186 | def __enter__(self): | ||
| 187 | return self | ||
| 188 | |||
| 189 | def __exit__(self, exc_type, exc_value, traceback): | ||
| 190 | self.close() | ||
| 191 | return False | ||
diff --git a/bitbake/lib/prserv/serv.py b/bitbake/lib/prserv/serv.py index c686b2065c..0db6ebc707 100644 --- a/bitbake/lib/prserv/serv.py +++ b/bitbake/lib/prserv/serv.py | |||
| @@ -344,9 +344,9 @@ def auto_shutdown(): | |||
| 344 | def ping(host, port): | 344 | def ping(host, port): |
| 345 | from . import client | 345 | from . import client |
| 346 | 346 | ||
| 347 | conn = client.PRClient() | 347 | with client.PRClient() as conn: |
| 348 | conn.connect_tcp(host, port) | 348 | conn.connect_tcp(host, port) |
| 349 | return conn.ping() | 349 | return conn.ping() |
| 350 | 350 | ||
| 351 | def connect(host, port): | 351 | def connect(host, port): |
| 352 | from . import client | 352 | from . import client |
