使用multiprocessing.Pool与异常处理

问题描述:

from multiprocessing import Pool 
def f(arg): 
    if arg == 1: 
     raise Exception("exception") 
    return "hello %s" % arg 

p = Pool(4) 
res = p.map_async(f,(1,2,3,4)) 
p.close() 
p.join() 
res.get() 

在那里我创建4名工人的进程池和f()分配工作考虑这个人为的例子。我的问题是:使用multiprocessing.Pool与异常处理

我怎样才能检索到这是对参数进行2,3,4成功的工作(并在同一时间做异常处理参数1)?

由于是代码只是给我:

Traceback (most recent call last): 
    File "process_test.py", line 13, in <module> 
    res.get() 
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/pool.py", line 567, in get 
    raise self._value 
Exception: exception 

可以使用imap功能。

iterator = p.imap(f, (1,2,3,4,5)) 

while True: 
    try: 
     print next(iterator) 
    except StopIteration: 
     break 
    except Exception as error: 
     print error