在 gRPC 中序列化对象
Serializing object in gRPC
我想从 gRPC 服务器 (localhost) 发送一些数据 (payload) 到我的客户端 (localhost)。
我调用 API(IMDB -> 前 250 部电影)并将我的数据保存在 JSON 文件中。
服务器响应必须是 IMDB 前 250 部电影(作为 JSON 对象)
但是我做不到
这是我的 .proto 文件:
syntax = "proto3";
// Incoming request from client
message get_top_250_movies{
optional string name = 1;
}
// Response to be returned by API service
message top_movies{
optional string response = 1;
}
// Service definition for MoviesData
service MoviesData{
// get movies method definition
rpc get_movies(get_top_250_movies) returns (top_movies) {};
}
客户端程序:
import grpc
import json
import movie_service_pb2 as pb2
import movie_service_pb2_grpc as pb2_grpc
with open('Top250Movies.json') as f:
data = json.load(f)
class FetchMovies:
def __init__(self) -> None:
self.channel = grpc.insecure_channel('localhost:50051')
self.stub = pb2_grpc.MoviesDataStub(self.channel)
def get_movies(self):
response = self.stub.get_movies(json.dumps(data))
return response
if __name__ == "__main__":
client = FetchMovies()
print(client.get_movies())
服务器程序:
import json
from concurrent import futures
import movie_service_pb2 as pb2
import movie_service_pb2_grpc as pb2_grpc
class MoviesDataService(pb2_grpc.MoviesDataServicer):
def get_movies(self, request, context):
with open('Top250Movies.json') as f:
data = json.load(f)
return pb2.get_top_250_movies(f'{request.name}')
def serve():
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
pb2_grpc.add_MoviesDataServicer_to_server(MoviesDataService(), server)
server.add_insecure_port("localhost:50051")
server.start()
server.wait_for_termination()
if __name__ == "__main__":
print('running the gRPC server')
serve()
我只需要接受任何请求并发回一个响应,其中包含我保存在 json 文件中的电影列表(将所有 250 部电影作为 JSON 对象发送给客户端)
json.dumps(pythonObject) 将面临错误:它无法序列化预期的 str |字节或...
感谢您的帮助
问题出在 .proto 文件上,我是这样自己修复的:
syntax = "proto3";
message MovieRequest {
// Send a Request to Server
string movie_list = 1;
}
// this message solved my problem
message MovieList {
// Get Movie data includes: rank, title, imDbRating, imDbRatingCount
string rank = 1;
string title = 2;
string imDbRating = 3;
string imDbRatingCount = 4;
}
message MovieResponse {
// Get a List of MovieList Message; field_name == movies
repeated MovieList movies = 1;
}
service Movies{
// Run method get_movies with MovieRequest parameter and return MovieResponse
rpc get_movies(MovieRequest) returns (MovieResponse);
}
我想从 gRPC 服务器 (localhost) 发送一些数据 (payload) 到我的客户端 (localhost)。 我调用 API(IMDB -> 前 250 部电影)并将我的数据保存在 JSON 文件中。 服务器响应必须是 IMDB 前 250 部电影(作为 JSON 对象) 但是我做不到
这是我的 .proto 文件:
syntax = "proto3";
// Incoming request from client
message get_top_250_movies{
optional string name = 1;
}
// Response to be returned by API service
message top_movies{
optional string response = 1;
}
// Service definition for MoviesData
service MoviesData{
// get movies method definition
rpc get_movies(get_top_250_movies) returns (top_movies) {};
}
客户端程序:
import grpc
import json
import movie_service_pb2 as pb2
import movie_service_pb2_grpc as pb2_grpc
with open('Top250Movies.json') as f:
data = json.load(f)
class FetchMovies:
def __init__(self) -> None:
self.channel = grpc.insecure_channel('localhost:50051')
self.stub = pb2_grpc.MoviesDataStub(self.channel)
def get_movies(self):
response = self.stub.get_movies(json.dumps(data))
return response
if __name__ == "__main__":
client = FetchMovies()
print(client.get_movies())
服务器程序:
import json
from concurrent import futures
import movie_service_pb2 as pb2
import movie_service_pb2_grpc as pb2_grpc
class MoviesDataService(pb2_grpc.MoviesDataServicer):
def get_movies(self, request, context):
with open('Top250Movies.json') as f:
data = json.load(f)
return pb2.get_top_250_movies(f'{request.name}')
def serve():
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
pb2_grpc.add_MoviesDataServicer_to_server(MoviesDataService(), server)
server.add_insecure_port("localhost:50051")
server.start()
server.wait_for_termination()
if __name__ == "__main__":
print('running the gRPC server')
serve()
我只需要接受任何请求并发回一个响应,其中包含我保存在 json 文件中的电影列表(将所有 250 部电影作为 JSON 对象发送给客户端) json.dumps(pythonObject) 将面临错误:它无法序列化预期的 str |字节或...
感谢您的帮助
问题出在 .proto 文件上,我是这样自己修复的:
syntax = "proto3";
message MovieRequest {
// Send a Request to Server
string movie_list = 1;
}
// this message solved my problem
message MovieList {
// Get Movie data includes: rank, title, imDbRating, imDbRatingCount
string rank = 1;
string title = 2;
string imDbRating = 3;
string imDbRatingCount = 4;
}
message MovieResponse {
// Get a List of MovieList Message; field_name == movies
repeated MovieList movies = 1;
}
service Movies{
// Run method get_movies with MovieRequest parameter and return MovieResponse
rpc get_movies(MovieRequest) returns (MovieResponse);
}