跳转到主要内容

Apache Thrift的纯Python实现。

项目描述

https://img.shields.io/codecov/c/github/Thriftpy/thriftpy2.svg https://img.shields.io/pypi/dm/thriftpy2.svg https://img.shields.io/pypi/v/thriftpy2.svg https://img.shields.io/pypi/pyversions/thriftpy2.svg https://img.shields.io/pypi/implementation/thriftpy2.svg

ThriftPy: https://github.com/eleme/thriftpy 已弃用,ThriftPy2旨在提供长期支持。

从Thriftpy迁移?

您只需要

import thriftpy2 as thriftpy

就是这样!thriftpy2完全兼容thriftpy。

安装

使用pip安装。

$ pip install thriftpy2

您也可以先安装cython,以在本地构建cython扩展。

$ pip install cython thriftpy2

代码示例

ThriftPy使使用thrift编写服务器/客户端代码变得非常简单。让我们来看看这个简单的pingpong服务示例。

我们需要一个‘pingpong.thrift’文件

service PingPong {
    string ping(),
}

然后我们可以创建一个服务器

import thriftpy2
pingpong_thrift = thriftpy2.load("pingpong.thrift", module_name="pingpong_thrift")

from thriftpy2.rpc import make_server

class Dispatcher(object):
    def ping(self):
        return "pong"

server = make_server(pingpong_thrift.PingPong, Dispatcher(), '127.0.0.1', 6000)
server.serve()

和一个客户端

import thriftpy2
pingpong_thrift = thriftpy2.load("pingpong.thrift", module_name="pingpong_thrift")

from thriftpy2.rpc import make_client

client = make_client(pingpong_thrift.PingPong, '127.0.0.1', 6000)
print(client.ping())

它还支持Python 3.5或更高版本的asyncio

import thriftpy2
import asyncio
from thriftpy2.rpc import make_aio_client


echo_thrift = thriftpy2.load("echo.thrift", module_name="echo_thrift")


async def request():
    client = await make_aio_client(
        echo_thrift.EchoService, '127.0.0.1', 6000)
    print(await client.echo('hello, world'))
    client.close()
import asyncio
import thriftpy2

from thriftpy2.rpc import make_aio_server

echo_thrift = thriftpy2.load("echo.thrift", module_name="echo_thrift")


class Dispatcher(object):
    async def echo(self, param):
        print(param)
        await asyncio.sleep(0.1)
        return param


def main():
    server = make_aio_server(
        echo_thrift.EchoService, Dispatcher(), '127.0.0.1', 6000)
    server.serve()


if __name__ == '__main__':
    main()

看,就这么简单!

您可以在源代码中的“examples”和“tests”目录中找到更多使用示例。

功能

目前ThriftPy具有以下功能(以及与上游python库相比的优势)

  • Python 3.6+和PyPy3。

  • 纯Python实现。不再需要编译和安装“thrift”包。您只需要thriftpy2和thrift文件。

  • 兼容Apache Thrift。您可以使用ThriftPy与官方实现的服务器和客户端一起使用,例如,使用thriftpy2客户端的上游服务器或反之亦然。

    当前实现的协议和传输

    • 二进制协议(Python和Cython)

    • 紧凑协议(Python和Cython)

    • JSON协议

    • 与Apache Thrift发行版的JSON协议兼容的Apache JSON协议。只需从thriftpy2.protocol import TApacheJSONProtocolFactory导入,并将此传递给适当的proto_factory参数。

    • 缓冲传输(Python和Cython)

    • 帧传输

    • tornado服务器和客户端(使用tornado 4.0)

    • HTTP服务器和客户端

    • asyncio支持(Python 3.5或更高版本)

  • 可以直接将thrift文件作为模块加载,SDK代码将实时生成。

    例如,pingpong_thrift = thriftpy2.load("pingpong.thrift", module_name="pingpong_thrift")将“pingpong.thrift”作为“pingpong_thrift”模块加载。

    或者,当通过thriftpy2.install_import_hook()启用导入钩子时,您可以直接使用import pingpong_thrift导入“pingpong.thrift”文件作为模块,您还可以使用from pingpong_thrift import PingService从thrift模块导入特定对象。

  • 简单的RPC服务器/客户端设置。

贡献

  1. 分叉存储库并进行更改。

  2. 编写一个测试用例,以显示已修复错误或功能按预期工作。

  3. 确保tox测试成功。

  4. 发送拉取请求。

贡献者

https://github.com/Thriftpy/thriftpy2/graphs/contributors

赞助商

./docs/jetbrains.svg

变更日志

https://github.com/Thriftpy/thriftpy2/blob/master/CHANGES.rst

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪一个,请了解有关 安装包 的更多信息。

源分发

thriftpy2-0.5.2.tar.gz (782.3 kB 查看哈希值)

上传时间

构建分发

thriftpy2-0.5.2-cp39-cp39-macosx_10_9_universal2.whl (1.0 MB 查看哈希值)

上传时间 CPython 3.9 macOS 10.9+ universal2 (ARM64, x86-64)

由支持