ray/doc/source/ray-core/tasks/patterns/too-many-results.rst
Eric Liang c8f207f746
[docs] Core docs refactor (#23216)
This PR makes a number of major overhauls to the Ray core docs:

Add a key-concepts section for {Tasks, Actors, Objects, Placement Groups, Env Deps}.
Re-org the user guide to align with key concepts.
Rewrite the walkthrough to link to mini-walkthroughs in the key concept sections.
Minor tweaks and additional transition material.
2022-03-17 11:26:17 -07:00

44 lines
1.4 KiB
ReStructuredText

Antipattern: Fetching too many results at once with ray.get
===========================================================
**TLDR:** Avoid calling ``ray.get`` on many large objects since this will lead to object store OOM. Instead process one batch at a time.
If you have a large number of tasks that you want to run in parallel, trying to do ray.get() on all of them at once could lead to object store OOM (out of memory). Instead you should process the results a batch at a time. Once one of the batches is processed, Ray will evict those objects preventing object store from running out of memory.
Code example
------------
**Antipattern:**
.. code-block:: python
@ray.remote
def return_big_object():
return np.zeros(1024*1024*10)
object_refs = [return_big_object.remote() for _ in range(1e6)]
# Calling ray.get will cause object store to OOM!
results = ray.get(object_refs)
write_to_file(results)
**Better approach:**
.. code-block:: python
@ray.remote
def return_big_object():
return np.zeros(1024*1024*10)
object_refs = [return_big_object.remote() for _ in range(1_000_000)]
for i in range(1_000):
chunk = object_refs[:1_000]
object_refs = object_refs[1_000:]
results = ray.get(chunk)
write_to_file(results)
.. figure:: too-many-results.svg
Fetching too many results at once with ``ray.get()``