Skip to main content

Pure-Python gRPC implementation for asyncio

Project description

https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/7e1631d13476f1e870af0d5605b643fc14471a6d/banner-direct-single.svg

project documentation version tag downloads license

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

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():
    async with Channel('127.0.0.1', 50051) as channel:
        greeter = GreeterStub(channel)

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


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 HelloReply
from .helloworld_grpc import GreeterBase


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 main(*, host='127.0.0.1', port=50051):
    server = Server([Greeter()])
    # Note: graceful_exit isn't supported in Windows
    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[protobuf]"

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=. --grpclib_python_out=. helloworld/helloworld.proto
                                                    ^----- note -----^

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

Plugin which implements --grpclib_python_out option should be available for the protoc compiler as the protoc-gen-grpclib_python executable which should be installed by pip into your $PATH during installation of the grpclib library.

Changed in v0.3.2: --python_grpc_out option was renamed into --grpclib_python_out.

Contributing

  • Please submit an issue before working on a Pull Request

  • Do not merge/squash/rebase your development branch while you work on a Pull Request, use rebase if this is really necessary

  • You may use Tox in order to test and lint your changes, but it is Ok to rely on CI for this matter

Project details


Release history Release notifications | RSS feed

This version

0.4.8

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.4.8.tar.gz (62.8 kB view details)

Uploaded Source

Built Distribution

grpclib-0.4.8-py3-none-any.whl (76.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: grpclib-0.4.8.tar.gz
  • Upload date:
  • Size: 62.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.8.18

File hashes

Hashes for grpclib-0.4.8.tar.gz
Algorithm Hash digest
SHA256 d8823763780ef94fed8b2c562f7485cf0bbee15fc7d065a640673667f7719c9a
MD5 c37be825d02fa3f3f488f172c784ca71
BLAKE2b-256 19750f0d3524b38b35e5cd07334b754aa9bd0570140ad982131b04ebfa3b0374

See more details on using hashes here.

File details

Details for the file grpclib-0.4.8-py3-none-any.whl.

File metadata

  • Download URL: grpclib-0.4.8-py3-none-any.whl
  • Upload date:
  • Size: 76.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.8.18

File hashes

Hashes for grpclib-0.4.8-py3-none-any.whl
Algorithm Hash digest
SHA256 a5047733a7acc1c1cee6abf3c841c7c6fab67d2844a45a853b113fa2e6cd2654
MD5 7cd3b3e8fcfd4bc73ec86eb3ad483b73
BLAKE2b-256 038bad381ec1b8195fa4a9a693cb8087e031b99530c0d6b8ad036dcb99e144c4

See more details on using hashes here.

Supported by

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