【问题标题】:gRPC API server via Python can't worked通过 Python 的 gRPC API 服务器无法工作
【发布时间】: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


    【解决方案1】:

    来自https://cloud.google.com/run/docs/issues#grpc_websocket:

    Cloud Run(全托管)目前不支持 HTTP 流式传输。因此,不支持使用 WebSockets 和 gRPC 流等协议的入站请求。

    【讨论】:

    • 我的 API 服务器只有 uanry 服务没有任何流媒体
    • 请发布有关如何调用服务器的示例代码。您确定服务器已启动并正常运行吗?您介意尝试其他应用吗(也许在 grpc.io 站点中部署 gRPC 快速入门示例)?
    • @AhmetB-Google 您对此有何看法?
    猜你喜欢
    • 1970-01-01
    • 2013-12-01
    • 1970-01-01
    • 2020-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多