thriftpy2 - Thrift RPC 框架的高性能 Python 实现
1. 项目简介
ThriftPy2 是 Thrift RPC 框架的 Python 实现,它是官方 Thrift Python 包的另一个实现,兼容 thriftpy
,并且提供了无需编译 .thrift
文件即可直接使用的功能。
ThriftPy2 的速度比 thriftpy
快许多,这可以从官方的 benchmark 中找到。
如果你需要使用 ThriftPy2,你需要安装 cython
和 thriftpy2
:
bash
pip install cython thriftpy2
由于 ThriftPy2 兼容 thriftpy
,所以你可以使用 thriftpy
的方式来使用 ThriftPy2:
python
import thriftpy2 as thriftpy
2. 快速开始
下面所使用的 Thrift 文件如下:
go
service PingPong {
string ping(),
}
基本示例:
python
import thriftpy2
from thriftpy2.rpc import make_server
pingpong_thrift = thriftpy2.load("pingpong.thrift", module_name="pingpong_thrift")
class Dispatcher(object):
def ping(self):
return "pong"
server = make_server(pingpong_thrift.PingPong, Dispatcher(), '127.0.0.1', 6000)
server.serve()
同样支持 asyncio
,使用上述 Thrift 文件,我们可以这样写:
python
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()