Skip to main content

Pure-Python gRPC implementation for asyncio

Project description

project documentation version tag license

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

Example

See examples directory in the project’s repository for all available examples.

Client

import asyncio

from grpclib.client import Channel

# generated by protoc
from .helloworld_pb2 import HelloRequest, HelloReply
from .helloworld_grpc import GreeterStub


async def main():
    channel = Channel('127.0.0.1', 50051)
    greeter = GreeterStub(channel)

    reply: HelloReply = await greeter.SayHello(HelloRequest(name='Dr. Strange'))
    print(reply.message)

    channel.close()


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

Server

import asyncio

from grpclib.utils import graceful_exit
from grpclib.server import Server

# generated by protoc
from .helloworld_pb2 import HelloRequest, HelloReply
from .helloworld_grpc import GreeterBase


class Greeter(GreeterBase):

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


async def main(*, host='127.0.0.1', port=50051):
    server = Server([Greeter()])
    with graceful_exit([server]):
        await server.start(host, port)
        print(f'Serving on {host}:{port}')
        await server.wait_closed()


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

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.

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.3.0rc3.tar.gz (39.1 kB view details)

Uploaded Source

File details

Details for the file grpclib-0.3.0rc3.tar.gz.

File metadata

  • Download URL: grpclib-0.3.0rc3.tar.gz
  • Upload date:
  • Size: 39.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.3.0rc3.tar.gz
Algorithm Hash digest
SHA256 617f31edbfa9f2279596926d28a69cb0443b69726f5fbc8ccb84df3d3201309c
MD5 7777de2eece197d7043405895ce6995a
BLAKE2b-256 f97896d97838a99451b38472a31981cdf2e1242dfd6509e86edf44a9e0dc44ef

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