V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
ray1888
V2EX  ›  程序员

thrift 接受一个对象列表的问题

  •  
  •   ray1888 ·
    ray1888 · 2017-09-30 16:57:57 +08:00 · 1714 次点击
    这是一个创建于 2399 天前的主题,其中的信息可能已经有所发展或是发生改变。

    刚刚公司在用 thrift 来搞一个项目,然后正在尝试接受一个列表,里面包含定义好的结构体, 那么为什么我直接用遍历列表不能取得结构体里面的元素

    c 端语言是 java,s 端语言是 python

    我在二楼放上 thrift 文件,3 楼放上代码,有大神能帮忙看一下吗?搜索了很多资料还是没有一个准确的解决方法

    5 条回复    2017-09-30 17:30:59 +08:00
    ray1888
        1
    ray1888  
    OP
       2017-09-30 17:01:12 +08:00
    ### thriftfile
    struct Span {
    1: i64 trace_id # unique trace id, use for all spans in trace
    2: string name,
    3: i64 id, # unique span id, only used for this span
    4: optional i64 parent_id, # parent span id
    5: list<Annotation> annotations, # all annotations/events that occured, sorted by timestamp
    6: list<BinaryAnnotation> binary_annotations # any binary annotations
    7: optional bool debug = 0 # if true, we DEMAND that this span passes all samplers
    8: optional i64 timestamp,
    9: optional i64 duration
    10: optional i64 trace_id_high
    }

    service Agent {
    oneway void emitZipkinBatch(1: list<Span> spans)
    }
    ray1888
        2
    ray1888  
    OP
       2017-09-30 17:02:16 +08:00
    ######server.py
    #!/usr/bin/python
    # coding=utf8
    import sys
    import os
    from socket import *

    from thrift import Thrift
    from thrift.transport import TSocket
    from thrift.transport import TTransport
    from thrift.protocol import TBinaryProtocol,TCompactProtocol
    from thrift.server import TServer

    sys.path.append(os.path.dirname(__file__)+"agent")
    sys.path.append(os.path.dirname(__file__)+"zipkincore")
    sys.path.append(os.path.dirname(__file__)+"jaeger")
    from agent import Agent
    from zipkincore.ttypes import Span
    from jaeger.ttypes import Batch

    class Handler(object):
    def emitZipkinBatch(spans):
    print(233)
    #for item in spans:
    #print item.trace_id
    #print item.name

    def emitBatch(batch):
    print(233)
    #for item in batch:


    def tbuffserver():
    transport = TSocket.TServerSocket(port=5775, socket_family=AF_INET)
    #transport = TSocket.TServerUDPSocket("127.0.0.1", 5775)
    tfactory = TTransport.TBufferedTransportFactory()
    pfactory = TBinaryProtocol.TBinaryProtocolFactory()
    #pfactory = TCompactProtocol.TCompactProtocolFactory()
    handler = Handler()
    processor = Agent.Processor(handler)
    server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)
    print('Starting the server...')
    server.serve()
    print('done.')


    def creatsock():
    sock = socket(AF_INET, SOCK_DGRAM)
    #sock.bind(("0.0.0.0", 6831))
    sock.bind(("0.0.0.0", 5775))
    while True:
    data = sock.recvfrom(65000)
    print data

    if __name__ == "__main__":
    #creatsock()
    tbuffserver()
    logger.warning(__name__)
    zgbgx1
        3
    zgbgx1  
       2017-09-30 17:07:24 +08:00
    我只尝试过用饿了么 thriftpy 官方文档中的 rpc 做过 demo,总体没啥问题,你这 thrift 是什么包
    ray1888
        4
    ray1888  
    OP
       2017-09-30 17:28:31 +08:00
    @zgbgx1 他原来是一个 uber 的一个分布式追踪的项目,我想服用它的 c 端,自己写 server 端
    zgbgx1
        5
    zgbgx1  
       2017-09-30 17:30:59 +08:00
    @ray1888 client 不是 java 吗,你这 server 端 python 建议直接用 https://github.com/eleme/thriftpy
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1047 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 22:14 · PVG 06:14 · LAX 15:14 · JFK 18:14
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.