【发布时间】:2022-02-09 02:29:06
【问题描述】:
我知道 google 声明 protobufs 不支持大消息 (i.e. greater than 1 MB),但我正在尝试使用数十兆字节的 gRPC 流式传输数据集,似乎有人说它是 ok,或至少与some splitting...
但是,当我尝试以这种方式 (repeated uint32) 发送数组时,在同一台本地计算机上大约需要 20 秒。
#proto
service PAS {
// analyze single file
rpc getPhotonRecords (PhotonRecordsRequest) returns (PhotonRecordsReply) {}
}
message PhotonRecordsRequest {
string fileName = 1;
}
message PhotonRecordsReply {
repeated uint32 PhotonRecords = 1;
}
PhotonRecordsReply 的长度需要约为 1000 万个 uint32...
有人知道如何加快速度吗?或者什么技术更合适?
所以我认为我已经实现了基于 cmets 和给出的答案的流式传输,但仍然需要相同的时间:
#proto
service PAS {
// analyze single file
rpc getPhotonRecords (PhotonRecordsRequest) returns (stream PhotonRecordsReply) {}
}
class PAS_GRPC(pas_pb2_grpc.PASServicer):
def getPhotonRecords(self, request: pas_pb2.PhotonRecordsRequest, _context):
raw_data_bytes = flb_tools.read_data_bytes(request.fileName)
data = flb_tools.reshape_flb_data(raw_data_bytes)
index = 0
chunk_size = 1024
len_data = len(data)
while index < len_data:
# last chunk
if index + chunk_size > len_data:
yield pas_pb2.PhotonRecordsReply(PhotonRecords=data[index:])
# all other chunks
else:
yield pas_pb2.PhotonRecordsReply(PhotonRecords=data[index:index + chunk_size])
index += chunk_size
最小重现 Github example
【问题讨论】:
-
您的示例中的
getPhotonRecordsRPC 不是流方法。您包含的参考更准确地说“不鼓励使用大于 1MB 的消息”,但流式传输代表“大型数据集”(许多较小的消息),并且使用这种方法是合适的。 -
发送包含
uint32重复(数组)的消息可能是“具有挑战性的”。uint32(以及 protos 中的其他整数编码)使用可变长度编码。见:developers.google.com/protocol-buffers/docs/proto#scalar -
@DazWilkin 是否编辑似乎应该与流媒体一起运行?它仍然很慢......
标签: protocol-buffers grpc proto