I'm looking a way to dynamically add arguments to a Pool of workers during the same iteration. So, in the case some of these fails I'm able to promptly re-process it.
from numpy import random
from multiprocessing import Pool
from time import sleep
def foo(x):
sleep(0.1)
# 50% chance to have a fault
return x, x if random.rand() > 0.5 else -1
random.seed(3) # seed
pool = Pool(2) # process
args = range(5) # arguments to process
for i,(id,x) in enumerate(pool.imap(foo, args)):
print i,x
if x != -1:
args.remove(id)
print args
The output is
0 0
1 1
2 2
3 3
4 -1
[4]
but I'd like it to be
0 0
1 1
2 2
3 3
4 -1
5, 4
[]
within the same iteration. I meant, I don't want to create a new map to the same Pool of workers once the iteration is complete. I'd like to push new argument directly, so that it fails at the first iteration I don't have to wait till the end before using the available process! I hope it make sense...
Update: My problem above simplified, the "foo" function takes about 20 minutes to complete and it's spread across 24 process which run concurrently. As soon one process fails I need to re-processing as soon as possible, as I don't want to wait 20 minutes when I have available resources.