Apache License 2.0

项目介绍

python-trio/purerpc 是一个托管在 GitHub 上的开源项目,主要使用 Python 编写,采用 Apache License 2.0 许可证,目前获得 228 个 Star、15 次 Fork。

仓库全名
python-trio/purerpc
开源许可证
Apache License 2.0
Star 数
228
Fork 数
15
未关闭 Issue
15

README 摘要

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…

完整内容见 GitHub 原始 README 或本页 。

常见问题

purerpc 用什么语言开发?

主要使用 Python 编写,采用 Apache License 2.0 许可证,目前有 228 个 Star。

purerpc 可以免费商用吗?

仓库采用 Apache License 2.0 许可证,可免费使用与修改,商业使用请遵守许可证条款(例如保留版权声明与许可声明)。

如何获取 purerpc 的源码?

可以直接克隆仓库:git clone https://github.com/python-trio/purerpc.git,默认分支为 master;安装与运行步骤见仓库 README。

purerpc 最近还在维护吗?

仓库最近一次更新为 2024年4月8日,当前有 15 个未关闭 Issue;Star 数为 228,可作为社区活跃度的参考。

关于

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

描述取自 GitHub 官方 API,中文内容由 GitRead 中文社区 编辑团队整理。

简要分析

热度分
267
价值分
141
活跃状态
活跃

语言占比

JavaScript
Python
Shell

Release

README

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.8
  • ? PyPy >= 3.8

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.

评论

暂无评论