I have a list of large generators like the following:
test_list = [(i for i in range(100000000)) for x in range(100)]
This is much larger than mine, but demonstrates reason for generator.
I want to evaluate a function on each generator independently:
def test_function(generator):
results = []
for i in range(3):
results.append(next(generator))
return results
For a function such as this, it makes sense to not evaluate the entire generator into a list before applying the function.
I want to run it in parallel:
import multiprocessing as mp
output = mp.Queue()
processes = [mp.Process(target=test_function, args=(generator, )) for generator in test_list]
# Run processes
for p in processes:
p.start()
# Exit the completed processes
for p in processes:
p.join()
# Get process results from the output queue
results = [output.get() for p in processes]
However, I get an error that the generator cannot be pickled.
What is a way that I can run this process in parallel?
Thanks, Jack