Home  >  Article  >  Backend Development  >  Analyze usage examples of tcp interactive thrift

Analyze usage examples of tcp interactive thrift

高洛峰
高洛峰Original
2017-03-26 16:36:162288browse

Apache Thrift is an efficient framework implemented by Facebook that supports remote service invocation in multiple programming languages.

  • Install thrift Download from the official website and install it according to ./configure, make, make install. For other systems, please refer to the official website http://thrift.apache.org/docs/install/

  • Define thrift file, which is equivalent to the interface definition of both communication parties.

    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
  • Use thrift to generate the thrift definition The code corresponds to various languages. I mainly use python, but other c++ and java can also be used. The command is thrift --gen

  • ##server and When writing the client, the server should consider non-blocking and multi-process to ensure normal service. The classes generated by thrift should be called to correspond to the defined data types.

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

The above is the detailed content of Analyze usage examples of tcp interactive thrift. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Python entry if statementNext article:Python entry if statement