Propagate computation resources available for child actor?

Here I set num_cpus=2 for spawn, but it could launch 4 sleepers in parallel, and all 4 sleepers start and end at the same time.

Is there a way to isolate num_cpus for a tasks and also its sub-tasks? In other words, in this example below, is there a way to have only 2 sleeper sleeping at a time?

I’m aware of ray.wait can achieve this but I’m wondering if there’s a way to force such resource limitation?

import ray
import time

ray.init(num_cpus=8)


@ray.remote
def sleeper(i):
    print(i, ' start sleeping')
    time.sleep(5)
    print(i, ' done sleeping')


@ray.remote(num_cpus=2)
def spawn():
    refs = [sleeper.remote(i) for i in range(4)]
    return ray.get(refs)


if __name__ == "__main__":
    print(ray.get(spawn.remote()))

The reason it run with 4 sleepers is because spawn (2 cpus ) + sleeper (1cpu) * 4 = 6 cpus.

So either you can increase sleeper to have 3 cpus each or you can give the placement group a try (Placement Groups — Ray v1.10.0)

Thank you for your reply. I was wondering if I can limit the number of concurrent sleepers by assigning num_cups=2 in spawn(). Do you think placement groups can achieve this?

I think so. You can create a placement group that can run 2 sleepers at most in parallel and schedule sleepers on that.