forked from ray-project/ray
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[core] fix workflow tests (ray-project#44994)
Fix workflow tests. The web service it used to compute fibonacci is completely dead now. Test: - CI - python python/ray/workflow/examples/comparisons/prefect/compute_fib_workflow.py Signed-off-by: can <can@anyscale.com>
- Loading branch information
1 parent
6588ed0
commit 6d6d0a1
Showing
1 changed file
with
23 additions
and
18 deletions.
There are no files selected for viewing
41 changes: 23 additions & 18 deletions
41
python/ray/workflow/examples/comparisons/prefect/compute_fib_workflow.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,36 @@ | ||
import tempfile | ||
|
||
import ray | ||
from ray import workflow | ||
import requests | ||
from ray.actor import ActorHandle | ||
|
||
|
||
@ray.remote | ||
class FibonacciActor: | ||
def __init__(self): | ||
self.cache = {} | ||
|
||
def fibonacci(n): | ||
assert n > 0 | ||
a, b = 0, 1 | ||
for _ in range(n - 1): | ||
a, b = b, a + b | ||
return b | ||
def compute(self, n): | ||
if n not in self.cache: | ||
assert n > 0 | ||
a, b = 0, 1 | ||
for _ in range(n - 1): | ||
a, b = b, a + b | ||
self.cache[n] = b | ||
return self.cache[n] | ||
|
||
|
||
@ray.remote | ||
def compute_large_fib(M: int, n: int = 1, fib: int = 1): | ||
try: | ||
next_fib = requests.post( | ||
"https://nemo.api.stdlib.com/fibonacci@0.0.1/", data={"nth": n} | ||
).json() | ||
assert isinstance(next_fib, int) | ||
except AssertionError: | ||
# TODO(suquark): The web service would fail sometimes. This is a workaround. | ||
next_fib = fibonacci(n) | ||
def compute_large_fib(fibonacci_actor: ActorHandle, M: int, n: int = 1, fib: int = 1): | ||
next_fib = ray.get(fibonacci_actor.compute.remote(n)) | ||
if next_fib > M: | ||
return fib | ||
else: | ||
return workflow.continuation(compute_large_fib.bind(M, n + 1, next_fib)) | ||
return workflow.continuation( | ||
compute_large_fib.bind(fibonacci_actor, M, n + 1, next_fib) | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
assert workflow.run(compute_large_fib.bind(100)) == 89 | ||
ray.init(storage=f"file://{tempfile.TemporaryDirectory().name}") | ||
assert workflow.run(compute_large_fib.bind(FibonacciActor.remote(), 100)) == 89 |