I would like to fun a function
using different arguments. For each different argument, I would like to run the function in parallel and then get the output of each run. It seems that the multiprocessing
module can help here. I am not sure about the right steps to make this work.
Do I start all the processes, then
get
all the queues and then join all the processes in this order? Or do Iget
the results after I have joined? Or do I get the ith result after I have joined the ith process?
from numpy.random import uniform
from multiprocessing import Process, Queue
def function(x):
return uniform(0.0, x)
if __name__ == "__main__":
queue = Queue()
processes = []
x_values = [1.0, 10.0, 100.0]
# Start all processes
for x in x_values:
process = Process(target=function, args=(x, queue, ))
processes.append(process)
process.start()
# Grab results of the processes?
outputs = [queue.get() for _ in range(len(x_values))]
# Not even sure what this does but apparently it's needed
for process in processes:
process.join()