File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 ('\n Successfully 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"\n sum_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"\n sum_remote(n)={ sum_remote (n )} " )
46+ total_time = time .time () - t0
47+ print (f"time sum_remote={ total_time } " )
Original file line number Diff line number Diff line change 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.
You can’t perform that action at this time.
0 commit comments