4

Reading multiprocessing.Pool doc I understood that map_async and apply_async are two versions of map and appy that are supposed to be faster, but that do not guarantee that the inputs are processed in the same order as they are provided.

However, I do not understand if, when calling multiprocessing.pool.AsyncResult.get() are the results "reordered" to match the input order, or are they returned in the order they were processed?

Darkonaut
  • 20,186
  • 7
  • 54
  • 65
Luca
  • 1,610
  • 1
  • 19
  • 30
  • I know by experience that the result is ordered, and the documentation suggests it, but surprisingly enough, this isn't explicitly mentioned. – Right leg Aug 30 '19 at 11:17
  • It is explicitly mentioned that `imap_unordered` is (shocker) ordered, so it could be a case of assumed ordered unless stated – Artog Aug 30 '19 at 11:18

2 Answers2

2

Yes, return-order will be the same as input-order. The only difference is that the async-methods do not block the MainThread in the parent and you will have to .get() the result explicitly. .map() and .map_async() both call the same low-level method ._map_async() under the hood.

Note that order of processing and return-order are two different things. Processing order is not guaranteed and influenced by chunking.

For .apply_async() you will get the result for which specific AsyncResult-object you are calling .get() upon.

Darkonaut
  • 20,186
  • 7
  • 54
  • 65
0

Performing the test below it seems that the order is restored when calling get(). As Right Leg said, however, I cannot find any mention to it in the doc. Therefore I won't accept this answer until this is not proved by a fact, rather than a test

import multiprocessing as mp
from time import sleep

def func(i):
    if i == 1:
        sleep(3)
    return i**2



if __name__=='__main__':

    with mp.Pool(mp.cpu_count()) as pool:
        res = pool.map_async(func, range(10)).get()
        print(res)     
Luca
  • 1,610
  • 1
  • 19
  • 30