thrift TSocket read 0 bytes(string类型中的一个坑)

来源:互联网 发布:软件开发解决方案总结 编辑:程序博客网 时间:2024/05/16 11:30

最近把thrif从0.9版本升级到0.10版本之后,一些一直在报thrift TSocket read 0 bytes 错误。
此篇文章记录thrift中的一个坑。
从thrift的官方文档中可以知道,如下:

Base Types
The base types were selected with the goal of simplicity
and clarity rather than abundance, focusing on the key types available
in all programming languages.

bool: A boolean value (true or false)
byte: An 8-bit signed integer
i16: A 16-bit signed integer
i32: A 32-bit signed integer
i64: A 64-bit signed integer
double: A 64-bit floating point number
string: A text string encoded using UTF-8 encoding

注意最后一句加粗的地方 A text string encoded using UTF-8 encoding。使用utf8编码的string类型。
因为官方手册给出的demo都是英文的string类型,所以运行起来是不会报错的。但是在我们的实际项目中,往往是有中文的。
因此,我这里做了一个统一处理:

def serialize_to(obj, tobj):    tobj = tobj()    for k, v in tobj.__dict__.iteritems():        value = getattr(obj, k)        if isinstance(value, datetime):            value = int(datetime2utc(value))        if isinstance(value, unicode):            # 注意这里            # value = value.encode('utf8')            pass        setattr(tobj, k, value)    return tobj

注意注释掉的部分,按照上面官方文档中说的,通过utf8对字符encode。结果server启动之后client访问就会报错TSocket read 0 bytes

最终一步一步排查,终于发现此问题。
版本升级要谨慎。!!

0 0