0

I have one function with multi arguments. and I need to run this function parallel for different inputs. how can I do that? I know the 'multiprocessing' module. but my problem is that for e.g pool. map accept just function with one argument. in other words, you think I have the below simple function. how can I run this function in parallel for different inputs?

def func(x,y,flag):
  if flag == 1:
     return x*y
  if flag == 2:
     return x+y
  if flag == 3:
     return x**y

1 Answers1

0

You can use Pool.starmap() like this

from multiprocessing import Pool

def func(x,y,flag):
  if flag == 1:
     return x*y
  if flag == 2:
     return x+y
  if flag == 3:
     return x**y
if __name__ == "__main__":
    with Pool() as p:
        res = p.starmap(func, [(1, 2, 3), (1, 2, 2),(1, 5, 1)])
        print(res)
Alexander Riedel
  • 1,329
  • 1
  • 7
  • 14