I want to iterate over some asynchronous results from an ipython parallel map as they arrive. The only way I can find to do this is to iterate over the results object. However if one of the tasks raises an exception the iteration terminates. Is there any way to do this? See code below, the iteration terminates when the second job raises an exception.
from IPython import parallel
def throw_even(i):
if i % 2 == 0:
raise RuntimeError('ERROR: %d' % i)
return i
rc = parallel.Client()
lview = rc.load_balanced_view() # default load-balanced view
# map onto the engines.
args = range(1, 5)
print args
async_results = lview.map_async(throw_even, range(1, 5), ordered=True)
# get results
args_iter = iter(args)
results_iter = iter(async_results)
while True:
try:
arg = args_iter.next()
result = results_iter.next()
print 'Job %s completed: %d' % (arg, result)
except StopIteration:
print 'Finished iteration'
break
except Exception as e:
print '%s: Job %d: %s' % (type(e), arg, e)
gives the following output that stops before jobs 3 and 4 are reported
[1, 2, 3, 4]
Job 1 completed: 1
<class 'IPython.parallel.error.RemoteError'>: Job 2: RuntimeError(ERROR: 2)
Finished iteration
Is there some way to do this?