带有列表的Python字典,如何在thrift存根代码中描述它?

时间:2012-03-15 09:43:46

标签: python rpc thrift

我正在尝试发送python字典{"1": ["2", 3, 4]}。服务器和客户端as here仅使用TSocket.TServerSocket(unix_socket="socket")TSocket.TSocket(unix_socket="socket") 相应的。该函数返回一个名为get_stats()的字典。

档案stats.thrift

struct Stat{
  1: required string login
  2: required i32 connections}
service Stats{
  map<string,list<Stat>> get_stats()}

生成代码 thrift --gen py stats.thrift

但是当调用该函数时,我在服务器端出错:

ERROR:root:'str' object has no attribute 'write'
Traceback (most recent call last):
  *more text*
  File "gen-py/stats/Stats.py", line 189, in write
    iter15.write(oprot)
AttributeError: 'str' object has no attribute 'write'

功能write()

def write(self, oprot):
    if oprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and self.thrift_spec is not None and fastbinary is not None:
      oprot.trans.write(fastbinary.encode_binary(self, (self.__class__, self.thrift_spec)))
      return
    oprot.writeStructBegin('get_stats_result')
    if self.success is not None:
      oprot.writeFieldBegin('success', TType.MAP, 0)
      oprot.writeMapBegin(TType.STRING, TType.LIST, len(self.success))
      for kiter13,viter14 in self.success.items():
        oprot.writeString(kiter13)
        oprot.writeListBegin(TType.STRUCT, len(viter14))
        for iter15 in viter14:
          iter15.write(oprot)
        oprot.writeListEnd()
      oprot.writeMapEnd()
      oprot.writeFieldEnd()
    oprot.writeFieldStop()
    oprot.writeStructEnd()

为什么thrift生成代码:

for iter15 in viter14:
  iter15.write(oprot)

问题:
为什么为类型write()调用方法str? 哪里我错了?
stats.thrift
在服务器或客户端代码?
thrift --gen命令?

1 个答案:

答案 0 :(得分:1)

在处理程序的返回代码中看起来像是一个问题。您可能正在使用字符串值而不是列表。你可以发布你的处理程序代码吗?