python - 通过 Python 的 gRPC API 服务器无法工作

我已经通过 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"

有人知道吗?因为当我在本地主机上测试时一切正常,即使进入 docker 容器仍然可以工作,但是当 Cloud Run 上的一切都崩溃时。

不确定此问题的根本原因是什么。它可能是 keepalive 或超时?

我在连接 channel 上尝试了很多事情,我将我的主机名更改为

 {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)

最佳答案

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

Cloud Run (fully managed) currently does not support HTTP streaming. Therefore, inbound requests with protocols like WebSockets and gRPC streaming are not supported.

https://stackoverflow.com/questions/63571365/

相关文章:

Python - 在 asyncio 中取消任务?

ruby-on-rails - 在 Rails 中进行 View 更新的正确方法

amazon-web-services - AWS API 网关上的剥离 header

php - 带有 "sometimes"规则的 Laravel Validator 自定义消息

python - 如何启用对 Dash DataTable 上特定行的编辑

java - 无法在数据库中使用 BCryptPassword 保存加密密码

c# - 如何从 .NET Core 3.1 中的 DI 获取 SignalR IHubContex

python-3.x - Python3 Beautifulsoup4 从多个容器兄弟中提取文本

git - 以编程方式为新的 Azure 存储库设置默认分支名称

java - 无法在 Linux 上的 Intellij 中指定 SDK