Skip to content

Commit 6475f11

Browse files
stephanie-wangangelinalgjjyao
authored
[docs] Add antipattern for nested ray.get (#43184)
Signed-off-by: Stephanie Wang <[email protected]> Signed-off-by: Stephanie wang <[email protected]> Signed-off-by: Jiajun Yao <[email protected]> Co-authored-by: angelinalg <[email protected]> Co-authored-by: Jiajun Yao <[email protected]>
1 parent 10ad3fa commit 6475f11

File tree

6 files changed

+59
-1
lines changed

6 files changed

+59
-1
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# __anti_pattern_start__
2+
import ray
3+
import time
4+
5+
6+
@ray.remote
7+
def f():
8+
return 1
9+
10+
11+
@ray.remote
12+
def pass_via_nested_ref(refs):
13+
print(sum(ray.get(refs)))
14+
15+
16+
@ray.remote
17+
def pass_via_direct_arg(*args):
18+
print(sum(args))
19+
20+
21+
# Anti-pattern: Passing nested refs requires `ray.get` in a nested task.
22+
ray.get(pass_via_nested_ref.remote([f.remote() for _ in range(3)]))
23+
24+
# Better approach: Pass refs as direct arguments. Use *args syntax to unpack
25+
# multiple arguments.
26+
ray.get(pass_via_direct_arg.remote(*[f.remote() for _ in range(3)]))
27+
# __anti_pattern_end__

doc/source/ray-core/patterns/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ This section is a collection of common design patterns and anti-patterns for wri
1717
tree-of-actors
1818
pipelining
1919
return-ray-put
20+
nested-ray-get
2021
ray-get-loop
2122
unnecessary-ray-get
2223
ray-get-submission-order
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
.. _nested-ray-get:
2+
3+
Anti-pattern: Calling ray.get on task arguments harms performance
4+
=================================================================
5+
6+
7+
**TLDR:** If possible, pass ``ObjectRefs`` as direct task arguments, instead of passing a list as the task argument and then calling :func:`ray.get() <ray.get>` inside the task.
8+
9+
When a task calls ``ray.get()``, it must block until the value of the ``ObjectRef`` is ready.
10+
If all cores are already occupied, this situation can lead to a deadlock, as the task that produces the ``ObjectRef``'s value may need the caller task's resources in order to run.
11+
To handle this issue, if the caller task would block in ``ray.get()``, Ray temporarily releases the caller's CPU resources to allow the pending task to run.
12+
This behavior can harm performance and stability because the caller continues to use a process and memory to hold its stack while other tasks run.
13+
14+
Therefore, it is always better to pass ``ObjectRefs`` as direct arguments to a task and avoid calling ``ray.get`` inside of the task, if possible.
15+
16+
For example, in the following code, prefer the latter method of invoking the dependent task.
17+
18+
.. literalinclude:: ../doc_code/anti_pattern_nested_ray_get.py
19+
:language: python
20+
:start-after: __anti_pattern_start__
21+
:end-before: __anti_pattern_end__
22+
23+
Avoiding ``ray.get`` in nested tasks may not always be possible. Some valid reasons to call ``ray.get`` include:
24+
25+
- :doc:`nested-tasks`
26+
- If the nested task has multiple ``ObjectRefs`` to ``ray.get``, and it wants to choose the order and number to get.

doc/source/ray-core/patterns/nested-tasks.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.. _task-pattern-nested-tasks:
1+
.. _nested-tasks:
22

33
Pattern: Using nested tasks to achieve nested parallelism
44
=========================================================

doc/source/ray-core/patterns/ray-get-loop.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,7 @@ Instead, we should first schedule all remote calls, which are then processed in
2828

2929
Other ``ray.get()`` related anti-patterns are:
3030

31+
- :doc:`nested-ray-get`
3132
- :doc:`unnecessary-ray-get`
3233
- :doc:`ray-get-submission-order`
34+
- :doc:`ray-get-too-many-objects`

doc/source/ray-core/patterns/ray-get-too-many-objects.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.. _ray-get-too-many-objects:
2+
13
Anti-pattern: Fetching too many objects at once with ray.get causes failure
24
===========================================================================
35

0 commit comments

Comments
 (0)