刚刚公司在用 thrift 来搞一个项目,然后正在尝试接受一个列表,里面包含定义好的结构体, 那么为什么我直接用遍历列表不能取得结构体里面的元素
c 端语言是 java,s 端语言是 python
我在二楼放上 thrift 文件,3 楼放上代码,有大神能帮忙看一下吗?搜索了很多资料还是没有一个准确的解决方法
1
ray1888 OP ### 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) } |
2
ray1888 OP ######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__) |
3
zgbgx1 2017-09-30 17:07:24 +08:00
我只尝试过用饿了么 thriftpy 官方文档中的 rpc 做过 demo,总体没啥问题,你这 thrift 是什么包
|
5
zgbgx1 2017-09-30 17:30:59 +08:00
@ray1888 client 不是 java 吗,你这 server 端 python 建议直接用 https://github.com/eleme/thriftpy
|