Thrift 的 Python 版本:ThriftPy

jopen 10年前發布 | 68K 次閱讀 ThriftPy WEB服務/RPC/SOA

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

項目主頁:http://www.baiduhome.net/lib/view/home/1403444273265

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!