Skip to main content

Pure-Python gRPC implementation for asyncio

Project description

This project is based on hyper-h2 and requires Python >= 3.5.

Licensed under BSD-3-Clause license. See LICENSE.txt

Note: Python 2.7 support is not planned, but you can use official grpcio library for projects with such requirements.

Installation

$ pip3 install grpclib protobuf

Bug fixes and new features are frequently published via release candidates:

$ pip3 install --upgrade --pre grpclib

For the code generation you will also need a protoc compiler, which can be installed with protobuf system package:

$ brew install protobuf  # example for macOS users
$ protoc --version
libprotoc ...

Or you can use protoc compiler from the grpcio-tools Python package:

$ pip3 install grpcio-tools
$ python3 -m grpc_tools.protoc --version
libprotoc ...

Note: grpcio and grpcio-tools packages are not required in runtime, grpcio-tools package will be used only during code generation.

Protoc plugin

In order to use this library you will have to generate special stub files using plugin provided, which can be used like this:

$ python3 -m grpc_tools.protoc -I. --python_out=. --python_grpc_out=. helloworld/helloworld.proto

This command will generate helloworld_pb2.py and helloworld_grpc.py files.

Plugin, which implements --python_grpc_out option is available for protoc compiler as protoc-gen-python_grpc executable, which will be installed by pip/setuptools into your $PATH during installation of the grpclib library.

Example

See example directory for a full example of the helloworld service. example/README.rst contains instructions about how to generate helloworld_pb2.py and helloworld_grpc.py files and how to run example.

Example basically looks like this (for Python>=3.7):

import asyncio

from grpclib.server import Server
from grpclib.client import Channel

from .helloworld_pb2 import HelloRequest, HelloReply
from .helloworld_grpc import GreeterBase, GreeterStub


class Greeter(GreeterBase):

    async def SayHello(self, stream):
        request = await stream.recv_message()
        message = f'Hello, {request.name}!'
        await stream.send_message(HelloReply(message=message))


async def test():
    loop = asyncio.get_event_loop()

    # start server
    server = Server([Greeter()], loop=loop)
    await server.start('127.0.0.1', 50051)

    # perform request
    channel = Channel('127.0.0.1', 50051, loop=loop)
    stub = GreeterStub(channel)
    response = await stub.SayHello(HelloRequest(name='World'))
    print(response.message)
    channel.close()

    # shutdown server
    server.close()
    await server.wait_closed()


if __name__ == '__main__':
    asyncio.run(test())

Where helloworld.proto contains:

syntax = "proto3";

package helloworld;

message HelloRequest {
  string name = 1;
}

message HelloReply {
  string message = 1;
}

service Greeter {
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

Contributing

Use Tox in order to test and lint your changes.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

grpclib-0.2.5.tar.gz (35.1 kB view details)

Uploaded Source

File details

Details for the file grpclib-0.2.5.tar.gz.

File metadata

  • Download URL: grpclib-0.2.5.tar.gz
  • Upload date:
  • Size: 35.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for grpclib-0.2.5.tar.gz
Algorithm Hash digest
SHA256 700e0d56ff7cb7a492da3c2f743ed0b996492e5fe89ed928e484fd8cc4ba83f0
MD5 4c32718d968fa9f75f7a8f77470aea1b
BLAKE2b-256 d53d773877b38a028160e851003ef06cc6cfd3dd5a3074297b707f1c63e86f07

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page