I am new to multiprocessing in Python and I am trying to do the following:
import os
from multiprocessing import Pool
from random import randint
def example_function(a):
new_numbers = [randint(1, a) for i in range(0, 50)]
with Pool(processes=os.cpu_count()-1) as pool:
results = pool.map(str, new_numbers)
return results
if __name__ == '__main__':
numbers = [randint(1, 50) for i in range(0, 50)]
with Pool(processes=os.cpu_count()) as pool:
results = pool.map(example_function, numbers)
print("Final results:", results)
However, when running this I get: "AssertionError: daemonic processes are not allowed to have children".
Interchanging either pool.map
for a for loop does make it work. E.g. for the second one:
results = []
for n in numbers:
results.append(example_function(n))
However, since both the outer and inner tasks are very intensive I would like to be able to parallelize both. How can I do this?