Skip to main content

Native, async Python gRPC client and server implementation supporting asyncio, uvloop, trio

Project description

purerpc

Build Status PyPI version Supported Python versions

purerpc is a native, async Python gRPC client and server implementation supporting asyncio, uvloop, and trio (achieved with anyio compatibility layer).

This project is in maintenance mode. Updates will primarily be limited to fixing severe bugs, keeping the package usable for actively developed projects, and easing maintenance.

For use cases limited to asyncio, consider the Python package published by the main grpc project instead.

Requirements

  • CPython >= 3.7
  • PyPy >= 3.7

NOTE: PyPy support is tentative, as the grpcio dependency doesn't officially suport it. (The dependency should be removed, since it's only used by tests, examples, and offline tools.)

Installation

Latest PyPI version:

pip install purerpc

Latest development version:

pip install git+https://github.com/python-trio/purerpc.git

By default purerpc uses asyncio event loop, if you want to use uvloop or trio, please install them manually.

protoc plugin

purerpc adds protoc-gen-purerpc plugin for protoc to your PATH enviroment variable so you can use it to generate service definition and stubs:

protoc --purerpc_out=. --python_out=. -I. greeter.proto

or, if you installed grpcio_tools Python package:

python -m grpc_tools.protoc --purerpc_out=. --python_out=. -I. greeter.proto

Usage

NOTE: greeter_grpc module is generated by purerpc's protoc-gen-purerpc plugin.

Server

from purerpc import Server
from greeter_pb2 import HelloRequest, HelloReply
from greeter_grpc import GreeterServicer


class Greeter(GreeterServicer):
    async def SayHello(self, message):
        return HelloReply(message="Hello, " + message.name)

    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)
    # NOTE: if you already have an async loop running, use "await server.serve_async()"
    import anyio
    anyio.run(server.serve_async)  # or set explicit backend="asyncio" or "trio"

Client

import purerpc
from greeter_pb2 import HelloRequest, HelloReply
from greeter_grpc import GreeterStub


async def gen():
    for i in range(5):
        yield HelloRequest(name=str(i))


async def listen():
    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__':
    # NOTE: if you already have an async loop running, use "await listen()"
    import anyio
    anyio.run(listen)  # or set explicit backend="asyncio" or "trio"

You can mix server and client code, for example make a server that requests something using purerpc from another gRPC server, etc.

More examples in misc/ folder

Project history

purerpc was originally written by Andrew Stepanov and used the curio async event loop. Later it was migrated to the anyio API, supporting asyncio, curio, uvloop, and trio (though curio support has since been dropped from the API).

After going a few years unmaintained, the project was adopted by the python-trio organization with the intent of ensuring a continued gRPC solution for Trio users.

Project details


Download files

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

Source Distribution

purerpc-0.7.0.tar.gz (30.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

purerpc-0.7.0-py3-none-any.whl (34.7 kB view details)

Uploaded Python 3

File details

Details for the file purerpc-0.7.0.tar.gz.

File metadata

  • Download URL: purerpc-0.7.0.tar.gz
  • Upload date:
  • Size: 30.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.8.12

File hashes

Hashes for purerpc-0.7.0.tar.gz
Algorithm Hash digest
SHA256 37a0516e48d77fe4e91f5b60ae57d7e0d3b7a556669b450b4a99daa5ea03282f
MD5 54bd572c7ead3f75be859b49dc6c1d1f
BLAKE2b-256 986f867d39fae966410832d03b824993fc533b782fd7906159efe7d764b5b0a2

See more details on using hashes here.

File details

Details for the file purerpc-0.7.0-py3-none-any.whl.

File metadata

  • Download URL: purerpc-0.7.0-py3-none-any.whl
  • Upload date:
  • Size: 34.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.8.12

File hashes

Hashes for purerpc-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1a95a634ab59a64d73180c4c40e96642e181ff8d652f39d077014ef5767f402e
MD5 3e153026551a392af604abe8dcb312f4
BLAKE2b-256 3087fd239b5fff7358c9ef1474ed0d30b1942f97c41bcd94e1813ce099b914c2

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