doc/source/ray-core/patterns/limit-running-tasks.rst
.. _core-patterns-limit-running-tasks:
In this pattern, we use :ref:resources <resource-requirements> 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 actor concurrency to infinite. Tasks that use more than 1 CPU (e.g., via multithreading) may experience slowdown due to interference from concurrent ones, but otherwise are safe to run.
However, tasks or actors that use more than their proportionate share of memory may overload a node and cause issues like OOM. If that is the case, we can reduce the number of concurrently running tasks or actors on each node by increasing the amount of resources requested by them. This works because Ray makes sure that the sum of the resource requirements of all of the concurrently running tasks and actors on a given node does not exceed the node's total resources.
.. note::
For actor tasks, the number of running actors limits the number of concurrently running actor tasks we can have.
You have a data processing workload that processes each input file independently using Ray :ref:remote functions <ray-remote-functions>.
Since each task needs to load the input data into heap memory and do the processing, running too many of them can cause OOM.
In this case, you can use the memory resource to limit the number of concurrently running tasks (usage of other resources like num_cpus can achieve the same goal as well).
Note that similar to num_cpus, the memory resource requirement is logical, meaning that Ray will not enforce the physical memory usage of each task if it exceeds this amount.
Without limit:
.. literalinclude:: ../doc_code/limit_running_tasks.py :language: python :start-after: without_limit_start :end-before: without_limit_end
With limit:
.. literalinclude:: ../doc_code/limit_running_tasks.py :language: python :start-after: with_limit_start :end-before: with_limit_end