Apache Thrift 的纯 python 实现。
项目描述
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 服务器/客户端设置。
贡献
分叉回购并进行更改。
编写一个测试,显示错误已修复或功能按预期工作。
确保travis-ci或tox测试成功。
发送拉取请求。
贡献者
赞助商:
变更日志
https://github.com/Thriftpy/thriftpy2/blob/master/CHANGES.rst
项目详情
thriftpy2-0.4.14.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 1758ccaeb2a40d8779b50cdd3d7a3b43e8c5752f21ad0a54ded7c251d05219e8 |
|
MD5 | c601d1d3f81cb49061b55e58e49aa166 |
|
布莱克2-256 | 1dd16b041449bd04b953294f3a070fc96bd8ce23ff81e96cc4c2920f7d555fe0 |
thriftpy2-0.4.14 -cp38-cp38-macosx_10_15_x86_64.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | b4aae6f6c1d8d12e63c45f68ec1a25267e7d3af1ced1e5a82cbabaaed4bcebc9 |
|
MD5 | 2700ce3f1c8d548ff991446558b90f37 |
|
布莱克2-256 | 9e2fe3fde100254cbb00b0496e8e2385f3f1a5c4b314215170612c6d498c3132 |