Skip to content

Commit 6eab78f

Browse files
committed
initial commit
0 parents  commit 6eab78f

File tree

3 files changed

+152
-0
lines changed

3 files changed

+152
-0
lines changed

01_ray-wait.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import ray
2+
import time
3+
import timeit
4+
5+
# simulate remote functions with
6+
# different execution times
7+
@ray.remote
8+
def remote_task(x):
9+
time.sleep(x)
10+
return x
11+
12+
# create a list of things
13+
things = list(range(10))
14+
# ensure that the futures won’t complete in order
15+
things.sort(reverse=True)
16+
17+
18+
# # GET
19+
20+
# ## get results when all results are available
21+
# def in_order():
22+
# ### use remote function to retrieve futures
23+
# futures = list(map(lambda x: remote_task.remote(x), things))
24+
# ### ray.get will block your function until all futures are returned
25+
# values = ray.get(futures)
26+
# ### loop over results and print
27+
# for v in values:
28+
# print(f" Completed {v}")
29+
# ### simulate some business logic
30+
# time.sleep(1)
31+
32+
33+
# ## call order and see how long it takes to complete
34+
# print("GET took: ", timeit.timeit(lambda: in_order(), number=1))
35+
36+
37+
# WAIT
38+
39+
# ## process as results become available
40+
# def as_available():
41+
# ### use remote function to retrieve futures
42+
# futures = list(map(lambda x: remote_task.remote(x), things))
43+
# ### while still futures left
44+
# while len(futures) > 0:
45+
# ### call ray.wait to get the next future
46+
# ready_futures, rest_futures = ray.wait(futures)
47+
# ### show progress
48+
# print(f"Ready {len(ready_futures)} rest {len(rest_futures)}")
49+
# ### print results
50+
# for id in ready_futures:
51+
# print(f'completed value {id}, result {ray.get(id)}')
52+
# ### simulate some business logic
53+
# time.sleep(1)
54+
# ### wait on the ones that are not yet available
55+
# futures = rest_futures
56+
57+
# ## call order and see how long it takes to complete
58+
# print("WAIT took: ", timeit.timeit(lambda: as_available(), number=1))
59+
60+
61+
62+
## same as above but with timeout and remote cancel
63+
def as_available():
64+
futures = list(map(lambda x: remote_task.remote(x), things))
65+
### while still futures left
66+
while len(futures) > 0:
67+
### call ray.wait to get the next future
68+
### but with a 10s timeout and always collect 5 results before returning anything
69+
ready_futures, rest_futures = ray.wait(futures, timeout=10, num_returns=5)
70+
# if we get back less than num_returns
71+
if len(ready_futures) < 1:
72+
print(f"Timed out on {rest_futures}")
73+
# cancel remote function, e.g. if task is using a lot of resources
74+
ray.cancel(*rest_futures)
75+
break
76+
for id in ready_futures:
77+
print(f'completed value {id}, result {ray.get(id)}')
78+
futures = rest_futures
79+
80+
## call order and see how long it takes to complete
81+
print("WAIT took: ", timeit.timeit(lambda: as_available(), number=1))

02_ray-pipeline.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import random
2+
import ray
3+
import time
4+
5+
# first remote function
6+
## retrieve a value from somewhere
7+
@ray.remote
8+
def generate_number(s: int, limit: int, sl: float) -> int :
9+
# create a random seed from value of 's'
10+
random.seed(s)
11+
# wait for value of 'sl'
12+
time.sleep(sl)
13+
# create a random number between '0' and the value of 'limit'
14+
return random.randint(0, limit)
15+
16+
# second remote function
17+
## take values and do some math
18+
@ray.remote
19+
def sum_values(v1: int, v2: int, v3: int) -> int :
20+
# take three integer and add them up
21+
return v1+v2+v3
22+
23+
# use first remote function return as input for second remote function
24+
print(ray.get(sum_values.remote(generate_number.remote(1, 10, .1), generate_number.remote(5, 20, .2), generate_number.remote(7, 15, .3))))

03_ray_nested_parallelism.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import random
2+
import ray
3+
import time
4+
5+
# retrieve a value from somewhere
6+
@ray.remote
7+
def generate_number(s: int, limit: int) -> int :
8+
# create a random seed from value of 's'
9+
random.seed(s)
10+
# simulate processing time
11+
time.sleep(.1)
12+
# create a random number between '0' and the value of 'limit'
13+
return random.randint(0, limit)
14+
15+
# invoke multiple remote functions and return the 'ObjectRef's
16+
@ray.remote
17+
def remote_objrefs():
18+
results = []
19+
for n in range(4):
20+
results.append(generate_number.remote(n, 4*n))
21+
return results
22+
23+
# store returned 'ObjectRef's from remote_objrefs()
24+
futures = ray.get(remote_objrefs.remote())
25+
26+
# invoke multiple remote functions and return the resulting values directly
27+
@ray.remote
28+
def remote_values():
29+
results = []
30+
for n in range(4):
31+
results.append(generate_number.remote(n, 4*n))
32+
return ray.get(results)
33+
34+
# print returned results
35+
print(ray.get(remote_values.remote()))
36+
37+
38+
# take returned 'ObjectRef's and get results
39+
while len(futures) > 0:
40+
ready_futures, rest_futures = ray.wait(futures, timeout=600, num_returns=1)
41+
# break when the return is smaller than num_returns
42+
if len(ready_futures) < 1:
43+
ray.cancel(*rest_futures)
44+
break
45+
for id in ready_futures:
46+
print(f'completed result {ray.get(id)}')
47+
futures = rest_futures

0 commit comments

Comments
 (0)