Skip to content

Commit 73e17f2

Browse files
author
dbuchaca
committed
feat: first test in ray
1 parent f3518d1 commit 73e17f2

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from __future__ import absolute_import
2+
from __future__ import division
3+
from __future__ import print_function
4+
5+
import ray
6+
import time
7+
from time import sleep
8+
9+
ray.init(num_cpus=10, ignore_reinit_error=True)
10+
print('\nSuccessfully imported ray!')
11+
12+
13+
def regular_function():
14+
sleep(2)
15+
return 1
16+
17+
@ray.remote
18+
def remote_function():
19+
sleep(2)
20+
return 1
21+
22+
def sum_serial(n):
23+
result = 0
24+
for _ in range(n):
25+
result += regular_function()
26+
return result
27+
28+
29+
def sum_remote(n):
30+
results = []
31+
for _ in range(n):
32+
results.append(remote_function.remote())
33+
return sum(ray.get(results))
34+
35+
36+
n = 10
37+
38+
t0 = time.time()
39+
print(f"\nsum_serial(n)={sum_serial(n)}")
40+
total_time = time.time() - t0
41+
print(f"time sum_serial={total_time}")
42+
43+
44+
t0 = time.time()
45+
print(f"\nsum_remote(n)={sum_remote(n)}")
46+
total_time = time.time() - t0
47+
print(f"time sum_remote={total_time}")

python_libraries/ray/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
3+
# Introduction to Ray
4+
5+
6+
Ray introduces the `ray.remote` decorator which turns a python function into a "remote function".
7+
Remote functions are "futures", a remote function `f` can runned with `f.remote()`.
8+
This will trigger the execution of the function but not block the runtime of the program (as an async function).
9+
10+
In `01_parallel_calls.py` one can see the execution of a remote function, where each call needs 2 seconds.
11+
The remote function runned 10 times takes approximately 2 seconds, whereas the serial takes around 20.

0 commit comments

Comments
 (0)