迭代AsyncMapResult,其中* some * tasks引发了异常

时间:2017-03-03 04:27:17

标签: python ipython-parallel

当使用ipyparallel集群并行处理任务时,当某些任务引发异常时,如何迭代AsyncMapResult?

我得到的只是例外,但我无法弄清楚如何获得成功完成的任务的结果。

以下是一些用于演示此问题的最低示例代码:

import os
from ipyparallel import Client

# Create the ipyparallel client and LoadBalancedView    
c = Client(os.path.expanduser('~/.ipython/profile_default/security/ipcontroller-client.json'))
lview = c.load_balanced_view()

# Define the function that will fail on x==True
def random_fail(x):
    if x:
        raise ValueError('haha')
    return 'yay'

# Make a random sample of True/False
import random
tf = (True, False)
random_choices = [
    random.choice(tf)
    for x in xrange(100)
]

# Send the tasks to the ipyparallel cluster.
# amr is our AsyncMapResult object.
amr = lview.map(random_fail, random_choices, ordered=True)
amr.wait_interactive()

# At this point I only see the exceptions,
# but none of the 'yay' strings
for r in amr:
    print r

# I could try indexing directly into the amr,
# but that still wouldn't work
for i in xrange(100):
    if not random_choices[i]: # this should *not* cause random_fail() to fail
        print amr[i]

0 个答案:

没有答案