Sharing Thrift sample code in Python

Source: Internet
Author: User
ApacheThrift is an efficient framework implemented by Facebook to support remote service calls in multiple programming languages. This article briefly introduces the architecture, development, and usage of ApacheThrift from the perspective of Python developers.


Preface

Apache Thrift is an efficient remote service calling framework implemented by Facebook that supports multiple programming languages. This article briefly introduces the architecture, development, and usage of Apache Thrift from the perspective of Python developers.

Thrift introduction Thrift network stackTransport
Transport network read/write (socket, http, etc.) abstraction, used to decouple with other thrift components. Transport interfaces include: open, close, read, write, flush, isOpen, and readAll. The Server needs ServerTransport (an abstraction of the listening socket) to receive client connections. the interfaces include listen, accept, and close. In python, the Transport implementation includes: TSocket, THttpServer, TSSLSocket, TTwisted, and TZlibTransport. There are also two decorators for adding functions to the existing Transport, TBufferedTransport (adding buffering) and TFramedTransport (adding frames ). The Tranport Factory Passed in when the server is created. these factories include: TTransportFactoryBase (directly returned without any modification), TBufferedTransportFactory (returned Transport with buffer) and TFramedTransportFactory (returns the Transport of the frame location ).
Protocol
Protocol is used to abstract data formats and serialize requests and responses during rpc calls. The implementation of TProtocol includes TJSONProtocol, TSimpleJSONProtocol, TBinaryProtocol, TBinaryPotocolAccelerated, and TCompactProtocol.
Processor
Processor reads and writes stream abstraction, and finally calls the handler compiled by the user to respond to the corresponding service. The specific Processor is generated by compiler. you need to implement the service implementation class.
Server
Server creates a Transport, input and output Protocol, and responds to the handler of the service, listens to the client request, and then delegates it to the processor for processing. TServer is a base class. constructor parameters include: 1) processor, serverTransport2) processor, serverTransport, transportFactory, protocolFactory3) processor, serverTransport, inputTransportFactory, outputTransportFactory, inputProtocolFactory, outputProtocolFactory TServer actually requires the parameters listed in 3), 1) and 2), which will cause the corresponding parameters to use the default value. The TServer sub-classes include TSimpleServer, TThreadedServer, TThreadPoolServer, TForkingServer, THttpServer, TNonblockingServer, and TProcessPoolServerTServer to start services and receive client requests.
Code generated
Constants. py: Contains all declared constants ttypes. py: declared struct, which implements specific serialization and deserialization SERVICE_NAME.py: the description file of the corresponding service, including: Iface: service Interface Definition Client: client rpc call pile
Usage
Thrift is actually easy to use. define IDL and implement the handler corresponding to the service (the method name, parameter list, and interface definition are consistent). finally, select each component. You need to select: Transport (usually socket, but buffed and framed decorations factory), Protocol, Server.
Example IDL file
/* Thrift interface definition file */service HelloService {string say (1: string msg )}

After editing the definition file, run the following command to generate the thrift file. You can move the hello directory to the current directory for later calling.

thrift -r -gen py hello.thrift
Server
# coding: utf-8"""thrift_client.py"""import socketimport sysfrom hello import HelloServicefrom hello.ttypes import *from thrift.transport import TSocketfrom thrift.transport import TTransportfrom thrift.protocol import TBinaryProtocolfrom thrift.server import TServerclass HelloServiceHandler:    def say(self, msg):        ret = "Received: " + msg        print ret        return rethandler = HelloServiceHandler()processor = HelloService.Processor(handler)transport = TSocket.TServerSocket("localhost", 9090)tfactory = TTransport.TBufferedTransportFactory()pfactory = TBinaryProtocol.TBinaryProtocolFactory()server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)print "Starting thrift server in python..."server.serve()print "done!"
Client
# coding: utf-8"""thrift_client.py"""import sysfrom hello import HelloServicefrom thrift import Thriftfrom thrift.transport import TSocketfrom thrift.transport import TTransportfrom thrift.protocol import TBinaryProtocoltry:    transport = TSocket.TSocket('localhost', 9090)    transport = TTransport.TBufferedTransport(transport)    protocol = TBinaryProtocol.TBinaryProtocol(transport)    client = HelloService.Client(protocol)    transport.open()    print "client - say"    msg = client.say("Hello!")    print "server - " + msg    transport.close()except Thrift.TException, ex:    print "%s" % (ex.message)
Running result
$ ptyhon thrift_client.pyclient - sayserver - Received: Hello!$ python thrift_server.pyStarting thrift server in python...Received: Hello!
Summary

This article is just a simple example. in actual projects, the thrift status of the service is generally registered and managed based on zookeeper, and further encapsulated on the server and client, it is easy to call in various modules of the project.

The above is a detailed description of the Thrift sample code in Python. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.