From 69433cab868041a20981471d0e5edc41b661c79e Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Mon, 13 Nov 2023 13:18:12 +0100 Subject: [PATCH] refactor: Change MuxRPC.__await__ MuxRPC.process_messages `__await__` can be a bit dangerous in the world of `asyncio`. --- examples/test_client.py | 2 +- ssb/muxrpc.py | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/examples/test_client.py b/examples/test_client.py index 624b53e..c08907b 100644 --- a/examples/test_client.py +++ b/examples/test_client.py @@ -105,7 +105,7 @@ async def main(keypair): packet_stream = PacketStream(client) await client.open() api.add_connection(packet_stream) - await gather(ensure_future(api), test_client()) + await gather(ensure_future(api.process_messages()), test_client()) if __name__ == "__main__": diff --git a/ssb/muxrpc.py b/ssb/muxrpc.py index 6244a73..f121e13 100644 --- a/ssb/muxrpc.py +++ b/ssb/muxrpc.py @@ -155,11 +155,15 @@ class MuxRPCAPI: self.handlers = {} self.connection = None - async def __await__(self): + async def process_messages(self): + """Continuously process incoming messages""" + async for req_message in self.connection: body = req_message.body + if req_message is None: return + if isinstance(body, dict) and body.get("name"): self.process(self.connection, MuxRPCRequest.from_message(req_message))