在使用gRPC做爲遠程調用框架時,如何使用python來表示多維數組呢?gRPC中定義proto文件時,有一個參數是repeated,用來表示重複的數據類型,使用這個參數能夠表示list類型。以下,我想表示一個三維數組,這個數組表示的是一個三通道RGB彩色圖像,proto文件以下所示,proto文件中GrayFeature表示的是一個通道的灰度圖像。python
syntax = "proto3"; service DataListServer { rpc TransDataList(stream GrayFeature) returns (Reply) {} } message GrayFeature { repeated Feature feature = 1; } message Feature { repeated int32 pixel = 2; }
對應的Python代碼以下:數組
data_list = ndarray.tolist() lenth = len(data_list) for j in range(0, lenth): matrix = data_list[j] gray_feature = data_list_pb2.GrayFeature() for column in range(0, len(matrix)): feature = gray_feature.feature.add() feature.pixel.extend(matrix[column]) yield gray_feature
在服務端對應的解析的Python代碼以下:框架
data_list = [[[]]] * 3 for gray_feature in features: j = 0 feature = gray_feature.feature length = len(feature) matrix = [[]] * length for i in range(0, length): matrix[i] = feature[i].pixel data_list[j] = matrix j += 1