From 7475c1cb7aa67a94567f0cc113935c617c32e4fc Mon Sep 17 00:00:00 2001 From: Lasse Blaauwbroek Date: Thu, 12 Oct 2023 10:00:31 +0200 Subject: [PATCH] Update README example to async code --- README.md | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 88c95dd..4fd6e71 100644 --- a/README.md +++ b/README.md @@ -174,6 +174,7 @@ if __name__ == '__main__': Also, pycapnp has gained RPC features that include pipelining and a promise style API. Refer to the calculator example in the examples directory for a much better demonstration: ```python +import asyncio import capnp import socket @@ -189,27 +190,29 @@ class Server(test_capability_capnp.TestInterface.Server): return str(i * 5 + self.val) -def server(write_end): - server = capnp.TwoPartyServer(write_end, bootstrap=Server(100)) - - -def client(read_end): +async def client(read_end): client = capnp.TwoPartyClient(read_end) cap = client.bootstrap() cap = cap.cast_as(test_capability_capnp.TestInterface) remote = cap.foo(i=5) - response = remote.wait() + response = await remote assert response.x == '125' - -if __name__ == '__main__': - read_end, write_end = socket.socketpair(socket.AF_UNIX) +async def main(): + client_end, server_end = socket.socketpair(socket.AF_UNIX) # This is a toy example using socketpair. # In real situations, you can use any socket. - server(write_end) - client(read_end) + client_end = await capnp.AsyncIoStream.create_connection(sock=client_end) + server_end = await capnp.AsyncIoStream.create_connection(sock=server_end) + + _ = capnp.TwoPartyServer(server_end, bootstrap=Server(100)) + await client(client_end) + + +if __name__ == '__main__': + asyncio.run(capnp.run(main())) ```