From 2e194ff76b6f54db0d17174b003b4f282ef39870 Mon Sep 17 00:00:00 2001 From: Kai Fricke Date: Fri, 27 Jan 2023 08:58:53 -0800 Subject: [PATCH 1/2] [core] Add generic `__ray_ready__` method to Actor classes Signed-off-by: Kai Fricke --- python/ray/actor.py | 7 +++++-- python/ray/tests/test_actor.py | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/python/ray/actor.py b/python/ray/actor.py index 2da99a470c9d..aff15919a44f 100644 --- a/python/ray/actor.py +++ b/python/ray/actor.py @@ -1316,11 +1316,14 @@ def _modify_class(cls): "'class ClassName(object):' instead of 'class ClassName:'." ) - # Modify the class to have an additional method that will be used for - # terminating the worker. + # Modify the class to have additional methods + # for checking actor alive status and to terminate the worker. class Class(cls): __ray_actor_class__ = cls # The original actor class + def __ray_ready__(self): + return True + def __ray_terminate__(self): worker = ray._private.worker.global_worker if worker.mode != ray.LOCAL_MODE: diff --git a/python/ray/tests/test_actor.py b/python/ray/tests/test_actor.py index 5a12a70a4656..5a4a2b5a2b2a 100644 --- a/python/ray/tests/test_actor.py +++ b/python/ray/tests/test_actor.py @@ -1108,6 +1108,20 @@ def check_file_written(): os.unlink(tmpfile.name) +def test_actor_ready(ray_start_regular_shared): + @ray.remote + class Actor: + pass + + actor = Actor.remote() + + with pytest.raises(TypeError): + # Method can't be called directly + actor.__ray_ready__() + + assert ray.get(actor.__ray_ready__.remote()) + + def test_return_actor_handle_from_actor(ray_start_regular_shared): @ray.remote class Inner: From 51a7d2a5c94dd117a5387a2c06af1016d99918e3 Mon Sep 17 00:00:00 2001 From: Kai Fricke Date: Fri, 27 Jan 2023 10:49:26 -0800 Subject: [PATCH 2/2] Fix test_actor_autocomplete Signed-off-by: Kai Fricke --- python/ray/tests/test_actor.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/python/ray/tests/test_actor.py b/python/ray/tests/test_actor.py index 5a4a2b5a2b2a..baaa7fa7137e 100644 --- a/python/ray/tests/test_actor.py +++ b/python/ray/tests/test_actor.py @@ -1162,7 +1162,12 @@ def method_one(self) -> None: assert methods == ["method_one"] all_methods = set(dir(f)) - assert all_methods == {"__init__", "method_one", "__ray_terminate__"} + assert all_methods == { + "__init__", + "method_one", + "__ray_ready__", + "__ray_terminate__", + } method_options = [fn for fn in dir(f.method_one) if not fn.startswith("_")]