I am trying to parallelize a loop which uses an infinite generator as input to collect some data and stops when a certain amount of data has been received.
My implementation is something like this.
class A:
def __iter__(self):
i = 0
while True:
yield i
i += 1
def procpar(x):
r = random.random()
print('Computing x =', x)
if r > 0.5
return [2 * x]
else:
return [2 * x, x ** 2]
with ProcessPoolExecutor(4) as pool:
out = []
x = A()
for res in pool.map(procpar, x):
out.extend(res)
if len(out) > 100:
break
Now, when I run it, I do get this output, after which it just hangs and nothing happend.
Computing x = 1
Computing x = 6
Computing x = 2
Computing x = 3
Computing x = 4
Computing x = 5
Looking into whats going on, is that the map
method is trying to unroll and generate data from the iterator x = A()
, so it is stuck in an infinite loop.
Any suggestions how to avoid being stuck in infinite loop. Ofcorse, I could call the iterator x
in chunks before feeding them to the process pool, but looking if someone may have better or more straightforward solution.