Skip to content

Instantly share code, notes, and snippets.

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
# 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
channel = grpc.insecure_channel("localhost:1234", options=channel_opts)
stub = helloworld_pb2_grpc.GreeterStub(channel)
while True:
# Some large message
msg = 'Hello' * 100000
response = stub.SayHello(helloworld_pb2.HelloRequest(name=msg), timeout=0.3)
except RpcError as e:
# Handle timeouts here
# Do something with response
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.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.