Pattern: Using resources to limit the number of concurrently running tasks#
In this pattern, you use resources to limit the number of concurrently running tasks.
By default, Ray tasks require 1 CPU each and Ray actors require 0 CPU each, so the scheduler limits task concurrency to the available CPUs and doesn’t limit actor concurrency. Some tasks use more than 1 CPU, for example through multithreading. These tasks may slow down because of interference from concurrent tasks, but are otherwise safe to run.
However, tasks or actors that use more than their proportionate share of memory may overload a node and cause issues such as out-of-memory (OOM) errors. If so, you can reduce the number of concurrently running tasks or actors on each node by increasing the amount of resources they request. This works because Ray ensures that the sum of the resource requirements of all concurrently running tasks and actors on a node doesn’t exceed the node’s total resources.
Note
For actor tasks, the number of running actors limits the number of actor tasks that can run concurrently.
Example use case#
You have a data processing workload that processes each input file independently using Ray remote functions. Because each task loads the input data into heap memory and processes it, running too many tasks at once can cause OOM errors. In this case, use the memory resource to limit the number of concurrently running tasks. Other resources, such as num_cpus, can achieve the same goal. Like num_cpus, the memory resource requirement is logical, meaning that Ray doesn’t enforce the physical memory usage of each task if it exceeds this amount.
Code example#
Without limit:
import ray
# Assume this Ray node has 16 CPUs and 16G memory.
ray.init()
@ray.remote
def process(file):
# Actual work is reading the file and process the data.
# Assume it needs to use 2G memory.
pass
NUM_FILES = 1000
result_refs = []
for i in range(NUM_FILES):
# By default, process task will use 1 CPU resource and no other resources.
# This means 16 tasks can run concurrently
# and will OOM since 32G memory is needed while the node only has 16G.
result_refs.append(process.remote(f"{i}.csv"))
ray.get(result_refs)
With limit:
result_refs = []
for i in range(NUM_FILES):
# Now each task will use 2G memory resource
# and the number of concurrently running tasks is limited to 8.
# In this case, setting num_cpus to 2 has the same effect.
result_refs.append(
process.options(memory=2 * 1024 * 1024 * 1024).remote(f"{i}.csv")
)
ray.get(result_refs)