This above code works fine but I don't fully understand it:
from multiprocessing import Pool,Manager
from itertools import chain
def calculate_primes():
ncore = 2
N = 50
with Manager() as manager:
input = manager.Queue()
output = manager.Queue()
with Pool(ncore) as p:
it = p.starmap_async(find_prime,[(input,output)])
for r in chunks(range(1,N),10): # chunks the list into sublists
input.put(r)
input.put(None)
it.wait() # if here: doesn't work! Why?
output.put(None)
it.wait() # mut be here to works fine!!
res = list(chain(*list(iter(output.get,None))))
return res
find_prime() is a function that returns a prime numbers list.
When p.starmap_async(somefunction,[(input,output)]) instruction is really executed? Why does this function need it.wait() instruction to be executed?
If no it.wait() instruction in function => it returns empty list [] If it.wait() instruction located before inut.put(None) => it seemns an infinite loop that I must kill.
Why? Thanks a lot.