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()