Home > Back-end >  What is the order of return value of the starmap pool function?
What is the order of return value of the starmap pool function?

Time:12-13

With the following code :

def myFoo(par1,par2):
    res=par1 par2
    return res

inputPar=[(1,2),(2,3),(4,5)]
with Pool() as pool:
    poolReturn=pool.starmap(myFoo, [(i) for i in inputPar])

can I be sure that the indice in poolReturn corresponds to the order the function has been called with the corresponding parameters set ?

That is :

poolReturn[0]=3 #1 2
poolReturn[1]=5 #2 3
poolReturn[2]=9 #4 5

To be sure, I wrote this code that I find "ugly"

def myFoo(poolNb,par1,par2):
    res=par1 par2
    return {poolNb:res}

inputPar=[[1,2],[2,3],[4,5]]
with Pool() as pool:
    poolReturn=pool.starmap(myFoo, [[i] j for i,j in enumerate(inputPar)])

inputParamNb=1
resFromInputParamNb=poolReturn[:][inputParamNb][inputParamNb]
resFromInputParamNb

Or even more ugly (with a key that is a string).

def myFoo(poolNb,par1,par2):
    res=par1 par2
    dictKey='PoolNb_' str(poolNb)
    return {dictKey:res}

inputPar=[[1,2],[2,3],[4,5]]
with Pool() as pool:
    poolReturn=pool.starmap(myFoo, [[i] j for i,j in enumerate(inputPar)])

inputParString='PoolNb_1'
res_poolNb=[i[inputParString] for i in poolReturn if inputParString in i.keys()][0]
res_poolNb

I suppose there is a better solution. Any help will be appreciated.

CodePudding user response:

Yes. Pool.starmap() - and Pool.map() - return a result list with results in left-to-right order of the function applied to the iterable of arguments. Same way, e.g., the built-in map(), and itertools.starmap(), work in this respect, although those return generators rather than lists.

The only function of this kind that does not guarantee result order is Pool.imap_unordered(), which returns an iterable which defines nothing about the order in which it returns results.

  • Related