• Stars
    star
    217
  • Rank 182,446 (Top 4 %)
  • Language
    Python
  • License
    Apache License 2.0
  • Created about 7 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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

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 with stub.SayHelloToMany(gen()) as stream:
            async for reply in stream:
                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.

More Repositories

1

trio

Trio – a friendly Python library for async concurrency and I/O
Python
6,058
star
2

trustme

#1 quality TLS certs while you wait, for the discerning tester
Python
501
star
3

trio-asyncio

a re-implementation of the asyncio mainloop on top of Trio
Python
188
star
4

async_generator

Making it easy to write async iterators in Python 3.5
Python
94
star
5

sniffio

Sniff out which async library your code is running under
Python
90
star
6

unasync

The async transformation code.
Python
88
star
7

hip

A new Python HTTP client for everybody
Python
79
star
8

trio-websocket

WebSocket client and server implementation for Python Trio
Python
70
star
9

trimeter

(not ready yet) A simple but powerful job scheduler for Trio programs
Python
54
star
10

pytest-trio

Pytest plugin for trio
Python
53
star
11

triopg

PostgreSQL client for Trio based on asyncpg
Python
45
star
12

outcome

Capture the outcome of Python function calls
Python
32
star
13

exceptiongroup

Python
27
star
14

trio-typing

Type hints for Trio and related projects
Python
27
star
15

sphinxcontrib-trio

Make Sphinx better at documenting Python functions and methods
Python
26
star
16

cookiecutter-trio

Quickstart template for Trio projects
Python
25
star
17

trio-talks

Talks, slides, and other similar resources for Trio
Jupyter Notebook
22
star
18

snekomatic

The code behind @trio-bot
Python
21
star
19

flake8-async

Highly opinionated linter for Trio code
Python
17
star
20

trio-amqp

Asynchronous messaging for snake people
Python
10
star
21

asyncgpio

A small library to access GPIO pins the Trio way. Linux 4.9+ only.
Python
10
star
22

hypothesis-trio

Hypothesis plugin for trio
Python
8
star
23

trzmq

Trio + ZeroMQ - vowels = trzmq
Python
5
star
24

trio-monitor

Monitor utility for trio
Python
4
star
25

trio-owfs

Access 1wire buses (via owserver)
Python
3
star
26

ahip

You're probably looking for https://github.com/python-trio/hip
Python
2
star
27

python-trio.github.io

Trio website
1
star