Thrift 的 Python 版本:ThriftPy
ThriftPy 是 Apache Thrift 的一个纯 Python 语言实现。Apache Thrift 是跨语言服务访问的框架。最早由非死book 开发,贡献给了Apache。通过接口定义语言(IDL),定义和创建服务,Thrift生成特定语言的可供server和client 访问的代码。
Thrfit 有着非常优秀的效率,无论是内存还是 传输效率上。
特性
-
Supports python2.7 to python3.4 and pypy.
-
Compatible with Apache Thirft. You can use ThriftPy together with the official implemention servers and clients, such as a upstream server with a thriftpy client or the opposite.
(Currently only binary protocol & buffered transport were implemented.)
-
Can directly load thrift file as module, the sdk code will be generated on the fly.
For example, pingpong = thriftpy.load("pingpong.thrift") will load 'pingpong.thrift' as 'pingpong' module.
Or, when import hook enabled, directly use import pingpong_thrift to import the 'pingpong.thrift' file as module.
-
Pure python, standalone implemention. No longer need to compile & install the 'thrift' package. All you need is python and thrift file.
-
Easy RPC server/client setup.
服务器端示例代码:
import thriftpy from thriftpy.rpc import make_server pingpong = thriftpy.load("pingpong.thrift") class Dispatcher(object): def ping(self): return "pong" server = make_server(pingpong.PingPong, Dispatcher(), '127.0.0.1', 6000) server.serve()
客户端:
import thriftpy from thrift.rpc import make_client pingpong = thriftpy.load("pingpong.thrift") client = make_client(pingpong.PingPong, '127.0.0.1', 6000) client.ping()