我在Python中玩多处理。我正在尝试确定如果工人提出例外情况,我会做些什么,所以我编写了以下代码:
def a(num):
if(num == 2):
raise Exception("num can't be 2")
print(num)
p = Pool()
p.map(a, [2, 1, 3, 4, 5, 6, 7, 100, 100000000000000, 234, 234, 5634, 0000])
输出
3
4
5
7
6
100
100000000000000
234
234
5634
0
multiprocessing.pool.RemoteTraceback:
"""
Traceback (most recent call last):
File "/usr/lib/python3.5/multiprocessing/pool.py", line 119, in worker
result = (True, func(*args, **kwds))
File "/usr/lib/python3.5/multiprocessing/pool.py", line 44, in mapstar
return list(map(*args))
File "<stdin>", line 3, in a
Exception: Error, num can't be 2
"""
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.5/multiprocessing/pool.py", line 260, in map
return self._map_async(func, iterable, mapstar, chunksize).get()
File "/usr/lib/python3.5/multiprocessing/pool.py", line 608, in get
raise self._value
Exception: Error, num can't be 2
如果您可以看到印刷的" 2"的数字,但为什么也不是1号?
NOTE :我在Ubuntu上使用Python 3.5.2
默认情况下,池创建许多工人等于您的核心数量。当其中一个工程流程死亡时,可能会使已撤销的工作失去工作。它也可能将输出留在一个永不被冲洗的缓冲区中。
带有 .map((的模式是处理工人中的异常并返回一些合适的错误值,因为 .map((的结果应该为一对一的输入。
from multiprocessing import Pool
def a(num):
try:
if(num == 2):
raise Exception("num can't be 2")
print(num, flush=True)
return num
except Exception as e:
print('failed', flush=True)
return e
p = Pool()
n=100
results = p.map(a, range(n))
print("missing numbers: ", tuple(i for i in range(n) if i not in results))
这是另一个问题,其中包含有关异常如何在多处理中传播的良好信息。