doc/source/ray-core/patterns/closure-capture-large-objects.rst
TLDR: Avoid closure capturing large objects in remote functions or classes, use object store instead.
When you define a :func:ray.remote <ray.remote> function or class,
it is easy to accidentally capture large (more than a few MB) objects implicitly in the definition.
This can lead to slow performance or even OOM since Ray is not designed to handle serialized functions or classes that are very large.
For such large objects, there are two options to resolve this problem:
ray.put() <ray.put> to put the large objects in the Ray object store, and then pass object references as arguments to the remote functions or classes ("better approach #1" below)Anti-pattern:
.. literalinclude:: ../doc_code/anti_pattern_closure_capture_large_objects.py :language: python :start-after: anti_pattern_start :end-before: anti_pattern_end
Better approach #1:
.. literalinclude:: ../doc_code/anti_pattern_closure_capture_large_objects.py :language: python :start-after: better_approach_1_start :end-before: better_approach_1_end
Better approach #2:
.. literalinclude:: ../doc_code/anti_pattern_closure_capture_large_objects.py :language: python :start-after: better_approach_2_start :end-before: better_approach_2_end