[Core] on_evicted callback
Description
A Ray API to register a callback triggered when a Ray object is evicted.
Use case
Suppose I have a Ray actor that can create a Ray object that associates with some non-serializable states. In the following example, the non-serializable state is a temporary directory.
class MyObject:
pass
_non_serializable_states = {}
@ray.remote
class MyCreatorActor:
def create(self):
ref = ray.put(MyObject())
_non_serializable_states[ref] = tempfile.mkdtemp()
return ref
I want the following on_evicted function to be triggered on the creator actor's worker process when ref is evicted, i.e., when the Ray distributed reference counting for the Ray object is zero.
def on_evicted(ref):
shutil.rmtree(_non_serializable_states[ref])
del _non_serializable_states[ref]
How can I modify MyCreatorActor.create to register on_evicted?
Is there a Ray API similar to weakref.ref(obj, callback) to register such a callback?
There is a question on StackOverflow about this issue: https://stackoverflow.com/questions/78421032/how-to-properly-clean-up-non-serializable-states-associated-with-a-ray-object
@Atry We currently don't have it. I think you can do the application layer reference counting for your use case.
I think it is not possible to reliably do the application layer reference counting.
Yes but if your application is simple, you can do application layer reference counting as a workaround for now to unblock you.