Does Ray remote functions return in correct order?

  • Medium: It contributes to significant difficulty to complete my task, but I can work around it.

Main Question

Do ray remote functions output results in correct order?

If I call

ray.get([do_some_work.remote(x) for x in range(2)]

and do_some_work(1) finishes earlier than do_some_work(0), will I get:

a) [do_some_work(1), do_some_work(0)]
b) [do_some_work(0), do_some_work(1)]

I would like to get option (b), so if the answer is (a), is there a way to make enforce that I get option (b)?

More details

Taking code from the Ray tutorial “Tips for first-time users”:

@ray.remote
def do_some_work(x):
    time.sleep(random.uniform(0, 4)) # Replace this with work you need to do.
    return x

data_list = ray.get([do_some_work.remote(x) for x in range(4)])

Since do_some_work takes a variable amount of time, will Ray know I want the results to be:

[do_some_work(0), do_some_work(1), do_some_work(2), do_some_work(2)]

or will it output in the order of whichever job finishes earlier?

It’ll be the order you expect. Basically, a = f(b) and a will always be the result of f(b).

data_list == [do_some_work(0), do_some_work(1), do_some_work(2), do_some_work(2)]
1 Like