Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core] Add generic __ray_ready__ method to Actor classes #31997

Merged
merged 3 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions python/ray/actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
21 changes: 20 additions & 1 deletion python/ray/tests/test_actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -1148,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("_")]

Expand Down