为什么gRPCPython看不到第一个字段



Python gRPC消息不序列化第一个字段。我更新了protos并多次清理了所有东西,但没有修复。您可以看到日志,settings_dictstop字段,但将这些字段传递给AgentSetting后,在日志和服务器端就看不到了。此外,我尝试手动传递stop字段,但看不到。令人烦恼的是,gRPC没有抛出任何异常——它接受stop字段,但没有发送到服务器——打印时在AgentSetting中也看不到它,但stop字段可以像agent_setting.stop一样访问。

这是我的原型文件:

syntax = 'proto3';
import "base.proto";
message ConnectionCheckRequest {
string host_name = 1;
}
message RecordChunkRequest {
string host_name = 1;
string name = 2;
bytes chunk = 3;
uint32 chunk_index = 4;
uint32 chunk_number = 5;
}
message AgentSetting {
bool stop = 1;
bool connection = 2;
bool registered = 3;
string image_mode = 4;
uint32 sending_fail_limit = 5;
uint64 limit_of_old_records = 6;
string offline_records_exceed_policy = 7;
}
message AgentSettingRequest {
AgentSetting agent_setting = 1;
string host_name = 2;
}
message AgentSettingResponse {
AgentSetting agent_setting = 1;
}
message AgentRegistryRequest {
AgentSetting agent_setting = 1;
string host_name = 2;
}
service Chief {
rpc agent_update_setting(AgentSettingRequest) returns (AgentSettingResponse) {};
rpc agent_registry(AgentRegistryRequest) returns (Response) {};
rpc send (stream RecordChunkRequest) returns (Response) {};
rpc connection_check (ConnectionCheckRequest) returns (Response) {};
}

这是我的代码片段:

def register():
try:
with insecure_channel(settings.server_address) as channel:
setting_dict = settings.dict()
logger.info(f'nSetting Dict: {setting_dict}')
agent_setting = AgentSetting(**setting_dict)
logger.info(f'nAgent Setting Instance: n{agent_setting}')
response = ChiefStub(channel).agent_registry(
AgentRegistryRequest(
agent_setting=agent_setting,
host_name=settings.host_name
)
)
return response
except Exception as e:
logger.exception(f'Register Error: {str(e)}')
return Response(success=False, message="failure")

日志:

|2020-05-05T18:33:56.931140+0300| |5480| |INFO| |reqs:register:28| 
Setting Dict: {'stop': False, 'connection': True, 'registered': False, 'image_mode': 'RGB', 'sending_fail_limit': 3, 'limit_of_old_records': 5368709120, 'offline_records_exceed_policy': 'OVERWRITE'}
|2020-05-05T18:33:56.932137+0300| |5480| |INFO| |reqs:register:32| 
Agent Setting Instance: 
connection: true
image_mode: "RGB"
sending_fail_limit: 3
limit_of_old_records: 5368709120
offline_records_exceed_policy: "OVERWRITE"

在proto3中,未设置的值和默认值被认为是等效的。

因此,stop: false被认为等同于完全省略stop

参见语言指南(proto3(

请注意,对于标量消息字段,一旦解析消息,就无法判断字段是显式设置为默认值(例如布尔值是否设置为false(还是根本没有设置:在定义消息类型时应记住这一点。例如,如果你不希望某些行为在默认情况下也发生,那么当设置为false时,不要有一个布尔值来切换某些行为。还要注意,如果标量消息字段设置为默认值,则该值将不会在连线上序列化。

请注意,这与proto2不同。

相关内容

最新更新