Here I set num_cpus=2
for spawn
, but it could launch 4 sleeper
s in parallel, and all 4 sleeper
s 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()))