通过Python的gRPC API服务器无法正常工作

问题描述

我已经通过Python开发了gRPC API服务器,然后将其构建到容器中并部署在Cloud Run上,

我的gRPC服务器正在与TensorFlow应用程序集成。实际上是图像ML的张量流,但这是一个示例代码,需要讨论。

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)

解决方法

来自here

Cloud Run(完全托管)当前不支持HTTP流。因此,不支持带有WebSockets和gRPC流之类协议的入站请求

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...