How do you set a timeout in Python's gRPC Library
timeout
is an optional keyword parameter on RPC invocation so you should change
health = self.grpc_client.Health(self.health_ping)
to
health = self.grpc_client.Health(self.health_ping, timeout=my_timeout_in_seconds)
.
You may also want to catch and handle timeouts differently than other errors. Sadly the documentation is not quite good on this topic, so here's what you have:
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
Timeout will return DEADLINE_EXCEEDED status_code.value.
To define a timeout on client side, add an optional parameter timeout=<timeout in seconds>
when you invoke a service function;
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)
Note a timeout situation will raise an exception