14 """The Python example of utilizing wait-for-ready flag."""
16 from concurrent
import futures
17 from contextlib
import contextmanager
24 helloworld_pb2, helloworld_pb2_grpc = grpc.protos_and_services(
27 _LOGGER = logging.getLogger(__name__)
28 _LOGGER.setLevel(logging.INFO)
34 tcp_socket = socket.socket(socket.AF_INET6)
36 tcp_socket = socket.socket(socket.AF_INET)
37 tcp_socket.bind((
'', 0))
38 address_tuple = tcp_socket.getsockname()
39 yield "localhost:%s" % (address_tuple[1])
52 bound_port = server.add_insecure_port(server_address)
53 assert bound_port ==
int(server_address.split(
':')[-1])
60 wait_for_ready=wait_for_ready)
61 message = response.message
63 assert rpc_error.code() == grpc.StatusCode.UNAVAILABLE
64 assert not wait_for_ready
68 _LOGGER.info(
"Wait-for-ready %s, client received: %s",
69 "enabled" if wait_for_ready
else "disabled", message)
77 transient_failure_event = threading.Event()
79 def wait_for_transient_failure(channel_connectivity):
80 if channel_connectivity == grpc.ChannelConnectivity.TRANSIENT_FAILURE:
81 transient_failure_event.set()
85 channel.subscribe(wait_for_transient_failure)
89 thread_disabled_wait_for_ready = threading.Thread(target=process,
91 thread_disabled_wait_for_ready.start()
93 thread_enabled_wait_for_ready = threading.Thread(target=process,
95 thread_enabled_wait_for_ready.start()
98 transient_failure_event.wait()
103 thread_disabled_wait_for_ready.join()
105 thread_enabled_wait_for_ready.join()
111 if __name__ ==
'__main__':
112 logging.basicConfig(level=logging.INFO)