Upgrade to Pro — share decks privately, control downloads, hide ads and more …

grpclib: pure-Python gRPC implementation

grpclib: pure-Python gRPC implementation

Vladimir Magamedov

April 28, 2018
Tweet

More Decks by Vladimir Magamedov

Other Decks in Programming

Transcript

  1. gRPC protocol = HTTP/2 + • Deadlines • Cancellation •

    Metadata (extensibility) • Declarative service definition language (.proto files) • Binary messages encoding (protobuf by default) • Streaming requests and responses • Authentication, Introspection, Load balancing x2
  2. gRPC request: HEADERS :method = POST :scheme = http :path

    = /cafe.CoffeeMachine/MakeLatte :authority = cafe.svc:50051 content-type = application/grpc+proto te = trailers DATA ... length-prefixed message(s) ... DATA
  3. gRPC response: HEADERS :status = 200 content-type = application/grpc+proto DATA

    ... length-prefixed message(s) ... DATA HEADERS # trailers grpc-status = 0 # OK
  4. • grpcio by Google • Python 2.7, >=3.4 • threads

    • C++ based core: server and client • grpclib by Me & contributors • Python >= 3.5 • async/await • pure-Python, based on awesome hyper-h2 project
  5. Definition syntax = "proto3"; package helloworld; service Greeter { rpc

    SayHello (HelloRequest) returns (HelloReply) {} } message HelloRequest { string name = 1; } message HelloReply { string message = 1; }
  6. class Greeter(GreeterBase): async def SayHello(self, stream): request = await stream.recv_message()

    message = 'Hello, {}!'.format(request.name) await stream.send_message(HelloReply(message=message)) server = Server([Greeter()], loop=loop) loop.run_until_complete(server.start('127.0.0.1', 50051)) Server
  7. channel = Channel('greeter.svc', 50051, loop=loop) stub = GreeterStub(channel) async def

    make_request(): response = await stub.SayHello(HelloRequest(name='World')) assert response.message == 'Hello, World!' Client