-1

In my code there are a number of functions called func1, func2, func3, and so on... I'm using multiprocessing pool to call the functions and make them run parallel. The code looks like this:

def func1():
  ...

def func2():
  ...

if __name__ == '__main__':
    with Pool(processes=2) as pool:
       r1 = pool.apply_async(func1, ())
       r2 = pool.apply_async(func2, ())

       var1 = r1.get(timeout=20)
       var2 = r2.get(timeout=20)

I need to increase the number of functions and I was thinking of using some king of loop to call those functions. How could this be done?

Nic
  • 1

1 Answers1

1

Use lists for an additional layer of indirection.

if __name__ == '__main__':
    with Pool(processes=2) as pool:
       funcs = [func1, func2]
       rs = [pool.apply_async(func, ()) for func in funcs]
       vars = [r.get(timeout=20) for r in rs]
Steven Rumbalski
  • 44,786
  • 9
  • 89
  • 119