def test_client_actor_ref_basics(ray_start_regular): with ray_start_client_server_pair() as pair: ray, server = pair @ray.remote class Counter: def __init__(self): self.acc = 0 def inc(self): self.acc += 1 def get(self): return self.acc counter = Counter.remote() ref = counter.actor_ref # Make sure ClientActorRef is a subclass of ActorID assert isinstance(ref, ClientActorRef) assert isinstance(ref, ActorID) # Invalid ref format. with pytest.raises(Exception): ClientActorRef(b"\0") actor_id = b"\0" * 16 fut = Future() fut.set_result(actor_id) server_ref = ActorID(actor_id) for client_ref in [ClientActorRef(actor_id), ClientActorRef(fut)]: client_members = { m for m in client_ref.__dir__() if not m.startswith("_") } server_members = { m for m in server_ref.__dir__() if not m.startswith("_") } assert client_members.difference(server_members) == {"id"} assert server_members.difference(client_members) == set() # Test __eq__() assert client_ref == ClientActorRef(actor_id) assert client_ref != ref assert client_ref != server_ref # Test other methods assert client_ref.__repr__() == f"ClientActorRef({actor_id.hex()})" assert client_ref.binary() == actor_id assert client_ref.hex() == actor_id.hex() assert not client_ref.is_nil()
def get_actor(self, name: str) -> ClientActorHandle: task = ray_client_pb2.ClientTask() task.type = ray_client_pb2.ClientTask.NAMED_ACTOR task.name = name ids = self._call_schedule_for_task(task) assert len(ids) == 1 return ClientActorHandle(ClientActorRef(ids[0]))
def persistent_load(self, pid): assert isinstance(pid, PickleStub) if pid.type == "Object": return ClientObjectRef(pid.ref_id) elif pid.type == "Actor": return ClientActorHandle(ClientActorRef(pid.ref_id)) else: raise NotImplementedError("Being passed back an unknown stub")
def get_actor(self, name: str, namespace: Optional[str] = None) -> ClientActorHandle: task = ray_client_pb2.ClientTask() task.type = ray_client_pb2.ClientTask.NAMED_ACTOR task.name = name task.namespace = namespace or "" ids = self._call_schedule_for_task(task, num_returns=1) assert len(ids) == 1 return ClientActorHandle(ClientActorRef(ids[0]))
def get_actor(self, name: str, namespace: Optional[str] = None) -> ClientActorHandle: task = ray_client_pb2.ClientTask() task.type = ray_client_pb2.ClientTask.NAMED_ACTOR task.name = name task.namespace = namespace or "" futures = self._call_schedule_for_task(task, 1) assert len(futures) == 1 handle = ClientActorHandle(ClientActorRef(futures[0])) # `actor_ref.is_nil()` waits until the underlying ID is resolved. # This is needed because `get_actor` is often used to check the # existence of an actor. if handle.actor_ref.is_nil(): raise ValueError(f"ActorID for {name} is empty") return handle
def test_client_actor_ref_basics(ray_start_regular): with ray_start_client_server_pair() as pair: ray, server = pair @ray.remote class Counter: def __init__(self): self.acc = 0 def inc(self): self.acc += 1 def get(self): return self.acc counter = Counter.remote() ref = counter.actor_ref # Make sure ClientActorRef is a subclass of ActorID assert isinstance(ref, ClientActorRef) assert isinstance(ref, ActorID) # Invalid ref format. with pytest.raises(Exception): ClientActorRef(b"\0") # Test __eq__() id = b"\0" * 16 assert ClientActorRef(id) == ClientActorRef(id) assert ClientActorRef(id) != ref assert ClientActorRef(id) != ActorID(id) assert ClientActorRef(id).__repr__() == f"ClientActorRef({id.hex()})" assert ClientActorRef(id).binary() == id assert ClientActorRef(id).hex() == id.hex() assert not ClientActorRef(id).is_nil()