zoukankan      html  css  js  c++  java
  • python thrift

    thirft使用socket进行数据传输,数据以特定的格式发送,接收方进行解析。我们定义好thrift的IDL文件后,就可以使用thrift的编译器来生成双方语言的接口、model,在生成的model以及接口代码中会有解码编码的代码。

    TTransport层 代表thrift的数据传输方式

    • TSocket: 阻塞式socket;
    • TFramedTransport: 以frame为单位进行传输,非阻塞式服务中使用;
    • TFileTransport: 以文件形式进行传输;

    TProtocol层 代表thrift客户端和服务端之间传输数据的协议,通俗讲就是客户端和服务端之间传输数据的格式(例如json等)

    • TBinaryProtocol: 二进制格式;
    • TCompactProtocol: 压缩格式;
    • TJSONProtocol: JSON格式;
    • TSimpleJSONProtocol: 提供只写的JSON协议;

    thrift支持的Server模型

    • TSimpleServer: 简单的单线程服务模型,常用于测试;
    • TThreadPoolServer: 多线程服务模型,使用标准的阻塞式IO;
    • TNonBlockingServer: 多线程服务模型,使用非阻塞式IO(需要使用TFramedTransport数据传输方式);
    • THsHaServer: THsHa引入了线程池去处理,其模型读写任务放到线程池去处理,Half-sync/Half-async处理模式,Half-async是在处理IO事件上(accept/read/write io),Half-sync用于handler对rpc的同步处理;
      # test_client: 
      socket = TSocket.TSocket(host, port)
      socket.setTimeout(400)
    
      transport = TTransport.TBufferedTransport(socket)       # 1 TTransport层: 数据传输方式
      protocol = TBinaryProtocol.TBinaryProtocol(transport)   # 2 TProtocol层: 传输数据格式
      client = Experiment.Client(protocol)                    # 3 Client
    
      transport.open()
      resp = client.method(req)
      transport.close()
    
      # test_server
      handler = Handler()
      processor = Experiment.Processor(handler)   # 封装了从输入数据流中读数据和向数据流中写数据的操作
      socket = TSocket.TServerSocket(host, port)
      transport = TTransport.TBufferedTransportFactory()
      protocol = TBinaryProtocol.TBinaryProtocolFactory()
      server = TServer.TThreadedServer(processor, socket, transport, protocol)
      server.serve()
    
  • 相关阅读:
    Python函数
    linux—shell 脚本编程
    python 内建函数
    列表解析式(List Comprehension)
    python标准库(datetime)
    python字典(dict)
    常用数据结构
    C 2010年笔试题
    C 2012年笔试题(保)
    C 2012年笔试题
  • 原文地址:https://www.cnblogs.com/bsszds930/p/13431482.html
Copyright © 2011-2022 走看看