Skip to main content

Apache Thrift 的纯 python 实现。

项目描述

https://travis-ci.com/Thriftpy/thriftpy2.svg?branch=develop 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.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()

看,就是这么简单!

您可以参考源代码中的“示例”和“测试”目录以获取更多使用示例。

特征

目前 ThriftPy 具有这些特性(也优于上游 python 库):

  • 支持 Python 2.7、Python 3.4+、PyPy 和 PyPy3。

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

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

    当前实现的协议和传输:

    • 二进制协议(python 和 cython)

    • 紧凑协议(python 和 cython)

    • json协议

    • 缓冲传输(python & cython)

    • 框架运输

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

    • http服务器和客户端

    • 异步支持(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. 确保travis-citox测试成功。

  4. 发送拉取请求。

贡献者

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

赞助商:

./docs/jetbrains.svg

变更日志

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

项目详情


下载文件

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

源分布

thriftpy2-0.4.14.tar.gz (361.7 kB 查看哈希

已上传 source

内置分布

thriftpy2-0.4.14-cp38-cp38-macosx_10_15_x86_64.whl (467.6 kB 查看哈希

已上传 cp38