|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +import ray |
| 5 | + |
| 6 | +""" |
| 7 | +@ray.remote |
| 8 | +class StatefulActor: |
| 9 | + def __init__(self): |
| 10 | + super().__setattr__("state", {}) # Initialize state |
| 11 | +
|
| 12 | + def __setattr__(self, key, value): |
| 13 | + print('key: '+ key + ', value:', value) |
| 14 | + if key.startswith('_') or key == 'state': |
| 15 | + super().__setattr__(key, value) |
| 16 | + else: |
| 17 | + # Set the value using remote call |
| 18 | + ray.get(self.set_value.remote(key, value)) |
| 19 | +
|
| 20 | + def __getattr__(self, key): |
| 21 | + print(key) |
| 22 | + # Dynamically fetch the value from the actor's state |
| 23 | + if key.startswith('_') or key == 'state': |
| 24 | + raise AttributeError(f"Attribute '{key}' not found") # Avoid infinite loop |
| 25 | +
|
| 26 | + return ray.get(self.get_value.remote(key)) |
| 27 | +
|
| 28 | + def foobar(self): |
| 29 | + self.invocation_count +=1 |
| 30 | + print("_______FOOOBAR________") |
| 31 | + print(self.invocation_count) |
| 32 | + #self.set_value('invocation_count', self.invocation_count + 1) |
| 33 | + return "foobar" |
| 34 | +
|
| 35 | + def set_value(self, key, value): |
| 36 | + self.state[key] = value |
| 37 | +
|
| 38 | + def get_value(self, key): |
| 39 | + return self.state.get(key, None) |
| 40 | +
|
| 41 | +""" |
| 42 | +import ray |
| 43 | + |
| 44 | +@ray.remote |
| 45 | +class StatefulActor: |
| 46 | + def __init__(self): |
| 47 | + # Initialize internal state attributes |
| 48 | + self._state = {} # Use a private variable for dynamic attributes |
| 49 | + self.invocation_count = 0 # Internal state attribute |
| 50 | + |
| 51 | + """ |
| 52 | + def __setattr__(self, key, value): |
| 53 | + print('set', key, value) |
| 54 | + # Allow normal assignment for internal attributes |
| 55 | + if '_' in key: |
| 56 | + # Directly set internal state attributes |
| 57 | + super().__setattr__(key, value) |
| 58 | + else: |
| 59 | + # Store dynamic attributes in the _state dictionary |
| 60 | + self._state[key] = value |
| 61 | +
|
| 62 | + def __getattr__(self, key): |
| 63 | + print('get', key) |
| 64 | + # Allow normal retrieval for internal attributes |
| 65 | + if '_' in key: |
| 66 | + return super().__getattribute__(key) |
| 67 | + # Return from dynamic state |
| 68 | + return self._state.get(key, None) |
| 69 | + """ |
| 70 | + |
| 71 | + def foobar(self): |
| 72 | + # Increment invocation_count normally |
| 73 | + print('will halt') |
| 74 | + breakpoint() |
| 75 | + self.invocation_count += 1 |
| 76 | + return self.invocation_count |
| 77 | + |
| 78 | + def get_invocation_count(self): |
| 79 | + return self.invocation_count |
| 80 | + |
| 81 | + def get_dynamic_value(self, key): |
| 82 | + return self._state.get(key, None) |
| 83 | + |
| 84 | + |
| 85 | +# Initialize Ray |
| 86 | +ray.init( |
| 87 | + runtime_env={ |
| 88 | + "env_vars": {"RAY_DEBUG": "1"}, |
| 89 | + } |
| 90 | +) |
| 91 | + |
| 92 | +# Create a detached actor under the namespace "my_namespace" |
| 93 | +actor = StatefulActor.options( |
| 94 | + name="stateful_actor", |
| 95 | + lifetime="detached", |
| 96 | + namespace="my_namespace" |
| 97 | +).remote() |
| 98 | + |
| 99 | +# breakpoint() |
| 100 | +""" |
| 101 | +# Use the abstracted way to set and get values dynamically |
| 102 | +actor.key1 = 'value1' # Abstracted assignment |
| 103 | +print(ray.get(actor.key1)) # Outputs: value1 |
| 104 | +
|
| 105 | +actor.key2 = 'value2' |
| 106 | +print(ray.get(actor.key2)) # Outputs: value2 |
| 107 | +
|
| 108 | +""" |
| 109 | +ray.shutdown() |
| 110 | + |
0 commit comments