Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
gRPC's helloworld example modified to demonstrate deadlines and keepalive TCP_USER_TIMEOUT introduced in 1.16
import grpc
import time
import helloworld_pb2
import helloworld_pb2_grpc
def run():
channel_opts = [
(
# Interval at which grpc will send keepalive pings
'grpc.keepalive_time_ms',
5000
),
(
# Amount of time grpc waits for a keepalive ping to be
# acknowledged before deeming the connection unhealthy and closing
# this also sets TCP_USER_TIMEOUT for the underlying socket to this value
'grpc.keepalive_timeout_ms',
1000
)
]
channel = grpc.insecure_channel("localhost:1234", options=channel_opts)
stub = helloworld_pb2_grpc.GreeterStub(channel)
while True:
# Some large message
msg = 'Hello' * 100000
try:
response = stub.SayHello(helloworld_pb2.HelloRequest(name=msg), timeout=0.3)
except RpcError as e:
# Handle timeouts here
pass
else:
# Do something with response
pass
time.sleep(0.01)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.