I would like to call N number of python functions in parallel, but am not sure how to go about this.
I have read this thread and have implemented some of the code already.
from multiprocessing import Process
def func1():
print 'func1: starting'
for i in xrange(10000000): pass
print 'func1: finishing'
def func2():
print 'func2: starting'
for i in xrange(10000000): pass
print 'func2: finishing'
def runInParallel(*fns):
proc = []
for fn in fns:
p = Process(target=fn)
p.start()
proc.append(p)
for p in proc:
p.join()
if __name__ == '__main__':
runInParallel(func1, func2)
That's the code from the link above. I've adapted that code to work with my situation without issue.
So with this code, I can obviously run two functions in parallel. I could just add more functions manually, (func1, func2, func3, func4, ...). That seems messy and unpythonic.
Is there a way to re-do this code to call any number of functions in parallel?