xyzyx
1
How severe does this issue affect your experience of using Ray?
- Medium: It contributes to significant difficulty to complete my task, but I can work around it.
For example, I have an x
in class A
, and class A
is a ray actor. Here is the code.
@ray.remote
class A:
def __init__(self):
self.x=10
a=A.remote()
How can I get x
without adding extra functions? a.x
seems not good.
And I try ray.get(a).x
and ray.get(a.x)
. Those also failed.
jjyao
2
You cannot. You need to add a getter function to do that.
2 Likes
While not direct attribute access, you can try this as a general getter/setter function:
@ray.remote
class Actor:
def __init__(self, *args, **kwargs):
self.x = 10
def _get_obj_name_recurse(self, name, obj):
name = name.split(".", maxsplit=1)
recurse = len(name) > 1
next_name = name[1] if recurse else ""
name = name[0]
obj = self if obj is None else obj
return obj, name, next_name, recurse
def get_remote_attr(self, __name: str, __obj: object | None = None):
obj, name, next_name, recurse = self._get_obj_name_recurse(__name, __obj)
next_obj = getattr(obj, name)
if recurse:
next_obj = self.get_remote_attr(next_name, next_obj)
return next_obj
def set_remote_attr(self, __name: str, __value: Any, __obj: object | None = None):
obj, name, next_name, recurse = self._get_obj_name_recurse(__name, __obj)
if recurse:
self.set_remote_attr(next_name, __value, obj)
if hasattr(obj, name):
setattr(obj, name, __value)
Then you can access attributes like so:
a = A.remote()
ray.get(a.get_remote_attr.remote("x"))