Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python pool.starmap with list of dictionaries?

Is there a function similar to pools.starmap that can be used with list of dictionaries?

Instead of : pools.starmap(func, iterable_of_tuple)

You would have: pools.starmapdict(func, iterable_of_dictionaries)

so that the function starmapdict would be responsible for unpacking the dictionary of argument of the function.

like image 471
Tiphaine Champetier Avatar asked Apr 18 '18 14:04

Tiphaine Champetier


1 Answers

The workaround I used is:

from multiprocessing import Pool
    
data = [{"name": "Delphi", "age": 13}, {"name": "Orion", "age":24}, {"name": "Asher"}, {"name": "Baccus"}]
    
def fun_wrapper(dict_args):
    return fun(**dict_args)
    
def fun(name="Iseult", age=30):
    print(name, age)
    
if __name__ == '__main__':
    with Pool(3) as pool:
        pool.map(fun_wrapper, data)

The fun_wrapper function is responsible for unpacking the dictionary and giving it to the fun function.

like image 53
Tiphaine Champetier Avatar answered Jul 28 '24 20:07

Tiphaine Champetier