【发布时间】:2020-12-13 16:52:49
【问题描述】:
我已经通过 Python 开发了 gRPC API 服务器,然后将其构建到容器并部署在 Cloud Run 上,
我的 gRPC 服务器与 TensorFlow 应用程序集成。实际上是图像 ML 的 tensorflow,但这是一个示例代码以供讨论。
class Calculator(calculator_pb2_grpc.CalculatorServiecer):
def Calculate(self, request, context):
try:
# Processing
return #protobuf message
except Exception as e:
logging.error(e)
def main():
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
calculator_pb2_grpc.add_CalculatorServiecer_to_server(Calculator(), server)
server.add_insecure_port('[::]:8080')
server.start()
logging.info("Listening on %s.", '8080')
server.wait_for_termination()
if __name__ == '__main__':
main()
但是当我像这样调用 gRPC 时总是出错
gRPC status code 14
ERROR:root:<_InactiveRpcError of RPC that terminated with:
status = StatusCode.UNAVAILABLE
details = "Socket closed"
ERROR:root:<_InactiveRpcError of RPC that terminated with:
status = StatusCode.UNAVAILABLE
details = "Connection reset by peer"
有人知道吗?因为当我在 localhost 上测试时一切正常,即使进入 docker 容器仍然可以工作,但是当 Cloud Run 上的一切都崩溃时。
不确定这个问题的根本原因是什么。可能是keepalive或超时?
我在连接通道尝试了很多事情,我将主机名更改为
{HOST}:443
请帮忙,谢谢
示例客户端代码
我将客户端应用程序构建为用于调用外部 gRPC 的 REST API 服务器, 此 REST API 服务器也部署在 Cloud Run 上。
CHANNEL = grpc.insecure_channel('<CLOUD_RUN_DOMAIN_WITHOUT_HTTP>:443')
STUB = calculator_pb2_grpc.CalculatorStub(CHANNEL)
@app.get("/add") # passing value via query string
def index_dest(first : float, second : float):
try:
res = STUB.Calculate(calculator_pb2.BinaryOperation(first_operand=first,second_operand=second,operation="ADD"))
return {"message" : res}
except Exception as e:
logging.error(e)
【问题讨论】:
标签: python google-cloud-platform grpc google-cloud-run grpc-python