def get_handle(self, endpoint_name: str, missing_ok: Optional[bool] = False) -> RayServeHandle: """Retrieve RayServeHandle for service endpoint to invoke it from Python. Args: endpoint_name (str): A registered service endpoint. missing_ok (bool): If true, then Serve won't check the endpoint is registered. False by default. Returns: RayServeHandle """ if not missing_ok and endpoint_name not in ray.get( self._controller.get_all_endpoints.remote()): raise KeyError(f"Endpoint '{endpoint_name}' does not exist.") routers = list(ray.get(self._controller.get_routers.remote()).values()) current_node_id = ray.get_runtime_context().node_id.hex() try: router_chosen = next( filter(lambda r: get_node_id_for_actor(r) == current_node_id, routers)) except StopIteration: logger.warning( f"When getting a handle for {endpoint_name}, Serve can't find " "a router on the same node. Serve will use a random router.") router_chosen = random.choice(routers) return RayServeHandle( router_chosen, endpoint_name, )
def check_handle_router_id(): client = serve.connect() handle = client.get_handle("hi") return get_node_id_for_actor(handle.router_handle)