Skip to main content

Asynchronous pure Python gRPC client and server implementation supporting asyncio, uvloop, curio and trio

Project description

purerpc

Build Status PyPI version Downloads

Asynchronous pure Python gRPC client and server implementation supporting asyncio, uvloop, curio and trio (achieved with anyio compatibility layer).

Requirements

  • CPython >= 3.5
  • PyPy >= 3.5

Installation

Latest PyPI version:

pip install purerpc

Latest development version:

pip install git+https://github.com/standy66/purerpc.git

By default purerpc uses asyncio event loop, if you want to use uvloop, curio 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.

Below are the examples for Python 3.6 and above which introduced asynchronous generators as a language concept. For Python 3.5, where native asynchronous generators are not supported, you can use async_generator library for this purpose. Just mark yielding coroutines with @async_generator decorator and use await yield_(value) and await yield_from_(async_iterable) instead of yield.

Server

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


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}")


server = Server(50055)
server.add_service(Greeter().service)
server.serve(backend="asyncio")  # backend can also be one of: "uvloop", "curio", "trio"

Client

import anyio
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 main():
    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__":
    anyio.run(main, backend="asyncio")  # backend can also be one of: "uvloop", "curio", "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

Release 0.4.1 (2019-07-22)

Features

  • remove undocumented use of raw_socket in anyio (6de2c9a)

Release 0.4.0 (2019-07-22)

Bug Fixes

Features

  • add state property to GRPCStream (0019d8c)
  • answer PING frames (c829901)
  • change MAX_CONCURRENT_STREAMS from 1000 to 65536 (d2d461f)
  • decouple h2 and grpclib logic (1f4e6b0)
  • support percent-encoded grpc-message header (c6636f4)
  • change default max message length to 32 MB

Release 0.3.2 (2019-02-15)

Bug Fixes

  • fix dependencies, remove some of anyio monkey patches (ac6c5c2)

Release 0.3.1 (2019-02-15)

Bug Fixes

  • fix pickling error in purerpc.test_utils._WrappedResult (9f0a63d)

Release 0.3.0 (2019-02-14)

Features

  • expose new functions in purerpc.test_utils (07b10e1)
  • migrate to pytest (95c0a8b)

BREAKING CHANGES

  • purerpc.test_utils.PureRPCTestCase is removed

Release 0.2.0 (2019-02-10)

Features

  • add backend option to Server.serve (5f47f8e)
  • add support for Python 3.5 (a681192)
  • improved exception handling in test utils (b1df796)
  • migrate to anyio (746b1c2)

BREAKING CHANGES

  • Server and test now use asyncio event loop by default, this behaviour can be changed with PURERPC_BACKEND environment variable
  • purerpc.Channel is removed, migrate to purerpc.insecure_channel async context manager (now supports correct shutdown)

Release 0.1.6

  • Allow passing request headers to method handlers in request argument
  • Allow passing custom metadata to method stub calls (in metadata optional keyword argument)

Release 0.1.5

  • Enforce SO_KEEPALIVE with small timeouts
  • Expose PureRPCTestCase in purerpc API for unit testing purerpc services

Release 0.1.4

  • Speed up protoc plugin

Release 0.1.3 [PyPI only]

  • Fix long description on PyPI

Release 0.1.2

  • Fix unit tests on Python 3.7

Release 0.1.0

  • Implement immediate mode

Release 0.0.1

  • Initial release

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.4.1.tar.gz (26.9 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.4.1-py3-none-any.whl (34.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: purerpc-0.4.1.tar.gz
  • Upload date:
  • Size: 26.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.3

File hashes

Hashes for purerpc-0.4.1.tar.gz
Algorithm Hash digest
SHA256 40101af6738075b3eb63cc0a92df75377d914dbaa74820c9cfb47f7f105b519e
MD5 ac99a35454a98962086c4426b930c2dc
BLAKE2b-256 0420fb663b35afa42ebf5c7989b056725739e1002e92c2890989bec23d783a4f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: purerpc-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 34.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.3

File hashes

Hashes for purerpc-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d11d18a0481dc2b3a270e20fc4aae08a404b21ed259acf0d1778b5ba1fa45929
MD5 2c3a3421303c819d3d9e9d8bf922f467
BLAKE2b-256 24ed74be58447f326ce40b39fb9c5c2d8602405890ed751272e5fd5a3bdfc71b

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