Skip to main content

purerpc

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

Installation

Latest PyPI version:

pip install purerpc[grpc]

NOTE: for PyPy, replace "grpc" with "grpc-pypy". Support is tentative, as grpc does not officially support PyPy.

Latest development version:

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

These invocations will include dependencies for the grpc runtime and generation of service stubs.

To install extra dependencies for running tests or examples, using the test_utils module, etc., apply the [dev] suffix (e.g. pip install purerpc[dev]).

protoc plugin

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

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

or, if you installed the 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.

Release files for purerpc 0.8.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for purerpc 0.8.0
File Size Uploaded
purerpc-0.8.0.tar.gz 30.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for purerpc 0.8.0
File Interpreter ABI Platform
purerpc-0.8.0-py3-none-any.whl Python 3 none any Details

Total release size: 64.7 kB

Release files / purerpc-0.8.0.tar.gz

Download URL purerpc-0.8.0.tar.gz
Size 30.0 kB
Tags Source
SHA-256 checksum
How to use checksums
704ae76f1cc2cd6fc7e679fbd52f1e8de2c03ae72ffb16b39e5c0092e44f628f
BLAKE2b-256 checksum
How to use checksums
8c6a64b719f34e16789feaea4a9190839efbefd8b1d351723e5391d84826011f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.7.7

Release files / purerpc-0.8.0-py3-none-any.whl

Download URL purerpc-0.8.0-py3-none-any.whl
Size 34.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b91d3e8e14f61ac7fe3ca99f9cbfee114e21612ea42366949bcfca3f891f1f7d
BLAKE2b-256 checksum
How to use checksums
c506a2c8b9d577965d1d4c2acae6bb8956c97429675952fcdac42a7ee50b6482
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.7.7

Release history Release notifications | RSS feed

This release

0.8.0 This release

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.1

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.4.1

2 release files

0.3.2

2 release files

0.2.0

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

1 release file

0.1.2

2 release files

0.1.0

2 release files

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page