Apache Thrift 是 Facebook 實現的一種高效的、支援多種程式語言的遠端服務呼叫的框架。
安裝thrift 從官網下載然後依照./configure,make,make install 方式進行安裝,其他系統可以參考官網http://thrift.apache.org/docs/install/
定義thrift文件,相當於通訊雙方的介面定義,
bool Boolean, one byte * i8 (byte) Signed 8-bit integer * i16 Signed 16-bit integer * i32 Signed 32-bit integer * i64 Signed 64-bit integer * double 64-bit floating point value * string String * binary Blob (byte array) * map<t1,t2> Map from one type to another * list<t1> Ordered list of one type * set<t1> Set of unique elements of one type
用thrift產生關於thrift定義的程式碼,對應各種語言,我這主要是python,也可以其他的c++,java,指令是thrift --gen
#server和client的編寫,server要多考慮無阻塞,多進程,保證服務正常,呼叫thrift產生的類,對應定義好的資料類型。
def init_handler(): """ sub-process init handler """ signal.signal(signal.SIGINT, signal.SIG_DFL) signal.signal(signal.SIGTERM, signal.SIG_DFL) signal.signal(signal.SIGHUP, signal.SIG_DFL) signal.signal(signal.SIGUSR1, debug)def main(): """ The main entrance """ port = sys.argv[1] signal.signal(signal.SIGINT, exit_signal_handler) signal.signal(signal.SIGTERM, exit_signal_handler) signal.signal(signal.SIGHUP, exit_signal_handler) signal.signal(signal.SIGUSR1, debug) gc.set_threshold(20000, 10, 10) handler = MaitreyaHandler() processor = Maitreya.Processor(handler) transport = TSocket.TServerSocket(port=port) tfactory = TTransport.TBufferedTransportFactory() pfactory = TBinaryProtocol.TBinaryProtocolFactory() # server = TProcessPoolServer(processor, transport, tfactory, pfactory) server = TGeventServer.TGeventProcessPoolServer(processor, transport, tfactory, pfactory) server.setNumWorkers(WORKER_NUM) server.setPostForkCallback(init_handler) print "server started..." server.serve()
以上是解析tcp交互thrift的使用實例的詳細內容。更多資訊請關注PHP中文網其他相關文章!