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
0

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.