ray icon indicating copy to clipboard operation
ray copied to clipboard

[Core] on_evicted callback

Open Atry opened this issue 1 year ago • 1 comments

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?

Atry avatar May 21 '24 16:05 Atry

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 avatar May 21 '24 16:05 Atry

@Atry We currently don't have it. I think you can do the application layer reference counting for your use case.

jjyao avatar May 28 '24 21:05 jjyao

I think it is not possible to reliably do the application layer reference counting.

Atry avatar May 31 '24 00:05 Atry

Yes but if your application is simple, you can do application layer reference counting as a workaround for now to unblock you.

jjyao avatar May 31 '24 16:05 jjyao