【问题标题】:How do you set a timeout in Python's gRPC Library如何在 Python 的 gRPC 库中设置超时
【发布时间】:2017-10-07 17:45:33
【问题描述】:

我有一个 grpc 服务器/客户端,今天偶尔会挂起,导致问题。这是从 Flask 应用程序调用的,该应用程序正在与后台工作进程签入以确保它处于活动状态/正常运行。为了向 gRPC 服务器发出请求,我有:

try:
        health = self.grpc_client.Health(self.health_ping)
        if health.message == u'PONG':
            return {
                u'healthy': True,
                u'message': {
                    u'healthy': True,
                    u'message': u'success'
                },
                u'status_code': 200
            }
except Exception as e:
        if str(e.code()) == u'StatusCode.UNAVAILABLE':
            return {
                u'healthy': False,
                u'message': {
                    u'healthy': False,
                    u'message': (u'[503 Unavailable] connection to worker '
                                 u'failed')},
                u'status_code': 200}
        elif str(e.code()) == u'StatusCode.INTERNAL':
            return {
                u'healthy': False,
                u'message': {
                    u'healthy': False,
                    u'message': (u'[500 Internal] worker encountered '
                                 u'an error while responding')},
                u'status_code': 200}
        return {
            u'healthy': False,
            u'message': {u'healthy': False, u'message': e.message},
            u'status_code': 500
        }

客户端是一个存根:

channel = grpc.insecure_channel(address)
stub = WorkerStub(channel)
return stub

原型是:

syntax = "proto3";

option java_multiple_files = true;
option java_package = "com.company.project.worker";
option java_outer_classname = "ProjectWorker";
option objc_class_prefix = "PJW";

package projectworker;

service Worker {
  rpc Health (Ping) returns (Pong) {}
}

// The request message containing PONG
message Ping {
  string message = 1;
}

// The response message containing PONG
message Pong {
  string message = 1;
}

使用此代码,我将如何添加超时以确保我始终可以响应而不是失败并挂起?

【问题讨论】:

    标签: python grpc


    【解决方案1】:

    timeout is an optional keyword parameter on RPC invocation 所以你应该改变

    health = self.grpc_client.Health(self.health_ping)

    health = self.grpc_client.Health(self.health_ping, timeout=my_timeout_in_seconds)

    .

    【讨论】:

    【解决方案2】:

    您可能还希望以不同于其他错误的方式捕获和处理超时。 遗憾的是,关于这个主题的文档不是很好,所以这就是你所拥有的:

    try:
        health = self.grpc_client.Health(self.health_ping, timeout=my_timeout_in_seconds)
    except grpc.RpcError as e:
        e.details()
        status_code = e.code()
        status_code.name
        status_code.value
    

    超时将返回 DEADLINE_EXCEEDED status_code.value。

    【讨论】:

      【解决方案3】:

      要在客户端定义超时,请在调用服务函数时添加可选参数timeout=<timeout in seconds>

      channel = grpc.insecure_channel(...)
      stub = my_service_pb2_grpc.MyServiceStub(channel)
      request = my_service_pb2.DoSomethingRequest(data='this is my data')
      response = stub.DoSomething(request, timeout=0.5)
      

      ?注意超时情况会引发异常

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-01-15
        • 2022-06-11
        • 1970-01-01
        • 1970-01-01
        • 2015-06-05
        • 2017-11-03
        • 2011-01-23
        • 2021-12-20
        相关资源
        最近更新 更多