From d27d011143d94aa28146a0deb8db50ba2974dc68 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 | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 88c95dd..56ff47a 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 @@ -185,31 +186,33 @@ class Server(test_capability_capnp.TestInterface.Server): def __init__(self, val=1): self.val = val - def foo(self, i, j, **kwargs): + async def foo(self, i, j, **kwargs): 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())) ```