【发布时间】:2021-11-20 16:11:39
【问题描述】:
我有一个包含重复标量值的 gRPC 定义。
例如
message Request {
bool dryrun = 1;
bool verbose = 2;
bool enabled = 3;
float ratio = 4;
int32 count = 5;
string somevar = 6;
bool withcondition = 7;
bool nocondition = 8;
float portion = 9;
int32 population = 10;
string xvars = 11;
string singleoption = 12;
repeated int32 multioptions = 13;
}
我注意到,当这个请求被发送到 Python 服务器时,mylist 被转换为 RepeatedScalarContainer,它不是 JSON 可序列化的。 Some SO questions建议我用
from google.protobuf.json_format import MessageToJson
serialized = MessageToJson(original)
将 protobuf 消息转换为有效的 JSON 对象。 但在我的情况下,对收到的 Python 对象执行上述操作会给我一个截断的对象
{
"enabled": true,
"ratio": 0.5,
"count": 100,
"somevar": "/input",
"withcondition": true,
"singleoption": "option1",
"multioptions": [
1,
3
]
}
可以看到缺少一堆消息属性。
作为一种解决方法,我仍然可以提取接收到的对象的属性来恢复数据并通过蛮力将它们转换为可序列化的 JSON,但这看起来很丑陋和不稳定。这是我的方式
def handle_request(request):
all_attrs = dir(request)
i_last_private_attr = next((a for a, attr in enumerate(reversed(all_attrs)) if attr.startswith('_')), None)
if not i_last_private_attr:
_logger.error('Failed to find any request')
return
i_first_arg_attr = len(all_attrs) - i_last_private_attr
req_dict = {attr: getattr(request, attr) for attr in all_attrs[i_first_arg_attr:]}
for k, v in req_dict.items():
if isinstance(v, Iterable) and not isinstance(v, (dict, list, tuple, str)):
req_dict[k] = list(req_dict[k])
我应该如何让接收到的原始 gRPC 请求像只包含内置原始值类型的 Python 对象一样工作?
【问题讨论】:
-
为了提高效率,grpc 通常不会传输包含默认值的字段。这就是他们消失的原因。接收方将恢复接收消息中的那些字段。
标签: python json grpc grpc-python grpc-node