How to know if code is running on ray worker?

How severe does this issue affect your experience of using Ray?

  • Low: It annoys or frustrates me for a moment.

For some code I want to have different behaviors when run locally or on ray worker. Is there a standard API / recommended way to do that?

Something like this:

def is_on_ray_worker():
    if "ray" in sys.modules:
        import ray

        if ray.is_initialized():
            ctx = ray.get_runtime_context()
            if ctx.worker.mode == ray.WORKER_MODE:
                return True
    return False

1 Like

Hmm we don’t have a “official way” to do this. I think the way you are doing is the best way to do it. Please feel free to create a feature request (or make a PR with your implementation!)