hrpc/README.md

133 lines
3.2 KiB
Markdown
Raw Normal View History

2020-08-09 02:05:14 +00:00
# hyper-rpc
2020-08-08 23:33:07 +00:00
2020-08-09 02:05:14 +00:00
[![Build Status](https://drone.autonomic.zone/api/badges/hyperpy/hyper-rpc/status.svg)](https://drone.autonomic.zone/hyperpy/hyper-rpc)
2020-08-08 23:37:19 +00:00
2020-08-08 23:33:07 +00:00
## Simple RPC with Protobuf Services
2020-08-09 02:27:28 +00:00
Uses [grpcio_tools](https://pypi.org/project/grpc-tools) and [purerpc](https://github.com/standy66/purerpc) under the hood.
2020-08-08 23:33:07 +00:00
## Install
```sh
2020-08-09 02:05:14 +00:00
$ pip install hyper-rpc
2020-08-08 23:33:07 +00:00
```
2020-08-10 12:57:58 +00:00
## Known Issues
2020-08-10 12:59:24 +00:00
- wontfix: generated service/stub files which import other `.proto` files will
not have correct Python 3 import syntax (see [grpc/grpc#9575](https://github.com/grpc/grpc/issues/9575)). One work-around
for this is to translate `import foo_pb2 as foo__pb2` to `from . import foo_pb2 as foo__pb2` manually. See this [makefile
target](https://github.com/hyperpy/hyperspace-rpc/blob/5f1f88106c56ac48d0d1414f63616ba2d8af5f5d/makefile#L3)
for some `sed` inspiration.
2020-08-10 12:57:58 +00:00
2020-08-08 23:33:07 +00:00
## Example
2020-08-09 02:29:44 +00:00
> **TLDR; See the [example](https://github.com/hyperpy/hyper-rpc/tree/master/example) directory**
2020-08-09 00:52:29 +00:00
2020-08-09 01:35:01 +00:00
Define an RPC service in a `greeter.proto`.
2020-08-08 23:33:07 +00:00
```protobuf
2020-08-09 01:35:01 +00:00
syntax = "proto3";
2020-08-09 00:52:29 +00:00
2020-08-09 01:35:01 +00:00
service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply) {}
rpc SayHelloGoodbye (HelloRequest) returns (stream HelloReply) {}
rpc SayHelloToMany (stream HelloRequest) returns (stream HelloReply) {}
rpc SayHelloToManyAtOnce (stream HelloRequest) returns (HelloReply) {}
2020-08-08 23:33:07 +00:00
}
2020-08-09 01:35:01 +00:00
message HelloRequest {
string name = 1;
}
message HelloReply {
string message = 1;
2020-08-08 23:33:07 +00:00
}
```
2020-08-09 02:05:14 +00:00
Then generate the services and stubs with `hyper-rpc`.
2020-08-08 23:33:07 +00:00
```sh
2020-08-09 01:35:01 +00:00
$ hrpc greeter.proto
2020-08-08 23:33:07 +00:00
```
2020-08-09 01:35:01 +00:00
This creates `greeter_gprc.py` (services) and `greeter_pb2.py` (stubs) files.
2020-08-08 23:33:07 +00:00
2020-08-09 01:48:05 +00:00
You can then write an async-ready server.
2020-08-08 23:33:07 +00:00
```python
2020-08-09 01:35:01 +00:00
"""Greeter server."""
from greeter_grpc import GreeterServicer
from greeter_pb2 import HelloReply, HelloRequest
from purerpc import Server
class Greeter(GreeterServicer):
async def SayHello(self, message):
2020-08-09 01:53:39 +00:00
return HelloReply(message=f"Hello {message.name}")
2020-08-09 01:35:01 +00:00
async def SayHelloToMany(self, input_messages):
async for message in input_messages:
yield HelloReply(message=f"Hello, {message.name}")
if __name__ == "__main__":
server = Server(50055)
server.add_service(Greeter().service)
server.serve(backend="trio")
2020-08-08 23:33:07 +00:00
```
2020-08-09 01:35:01 +00:00
And a client.
2020-08-08 23:33:07 +00:00
```python
2020-08-09 01:35:01 +00:00
"""Greeter client."""
import anyio
import purerpc
from greeter_grpc import GreeterStub
from greeter_pb2 import HelloReply, HelloRequest
async def gen():
for i in range(5):
yield HelloRequest(name=str(i))
async def main():
async with purerpc.insecure_channel("localhost", 50055) as channel:
stub = GreeterStub(channel)
reply = await stub.SayHello(HelloRequest(name="World"))
print(reply.message)
async for reply in stub.SayHelloToMany(gen()):
print(reply.message)
if __name__ == "__main__":
anyio.run(main, backend="trio")
2020-08-08 23:33:07 +00:00
```
2020-08-09 00:52:29 +00:00
And run them in separate terminals to see the output.
2020-08-08 23:33:07 +00:00
```
$ python server.py # terminal 1
$ python client.py # terminal 2
```
2020-08-09 00:57:20 +00:00
2020-08-09 01:35:01 +00:00
Output:
```
Hello, World
Hello, 0
Hello, 1
Hello, 2
Hello, 3
Hello, 4
```
2020-08-09 00:57:20 +00:00
Go forth and [Remote Procedure Call](https://en.wikipedia.org/wiki/Remote_procedure_call).
![The person who invented the term RPC](https://upload.wikimedia.org/wikipedia/en/9/90/BruceJayNelson.JPG)