使用带有异常处理的multiprocessing.Pool

时间:2017-06-28 23:36:57

标签: python-2.7 process parallel-processing 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()中分配工作。我的问题是:

如何检索为参数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

2 个答案:

答案 0 :(得分:1)

您可以使用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

答案 1 :(得分:0)

您也可以在工作功能中执行错误处理

def do_work(x):
    try:
        return (None, something_with(x))
    except Exception as e:
        return (e, None)

output = Pool(n).map(do_work, input)

for exc, result in output:
    if exc:
        handle_exc(exc)
    else:
        handle_result(result)