设置在Python的GRPC图书馆

问题描述:

超时我有一个GRPC服务器/客户端,今天偶尔会死机,导致问题怎么办。这是从Flask应用程序中调用的,该应用程序正在使用后台工作进程检查以确保其处于活动状态。为了使请求GRPC服务器,我有:设置在Python的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; 
} 

使用此代码,我会怎样然后添加一个超时,以确保我可以随时响应而不是失败并挂起?

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)

+0

啊,好的。非常感谢你提供这些信息!不能相信我错过了这一点 – kkirsche