my multiprocessing.Pool(5).map(func, iterable)
does not return because one process is raising an exception. How can I continue execution after this exception raises?
I hoped moving to map_async
would solve this (maybe with the _error_callback
), but I had the same issue there.
Example code:
x.py
#!/usr/bin/env python3
from os import getpid
import multiprocessing as mp
def f(x):
print(x**2, f'Hi Im {getpid()}')
if not x:
raise Exception(f'bla {x}')
pool = mp.Pool(5)
res = pool.map(f, range(5))
print(res)
Output:
0 Hi Im 15701
1 Hi Im 15702
4 Hi Im 15703
9 Hi Im 15704
16 Hi Im 15705
multiprocessing.pool.RemoteTraceback:
"""
Traceback (most recent call last):
File "/usr/local/lib/python3.8/multiprocessing/pool.py", line 125, in worker
result = (True, func(*args, **kwds))
File "/usr/local/lib/python3.8/multiprocessing/pool.py", line 48, in mapstar
return list(map(*args))
File "./x.py", line 8, in f
raise Exception(f'bla {x}')
Exception: bla 0
"""
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "./x.py", line 13, in <module>
print(res.get())
File "/usr/local/lib/python3.8/multiprocessing/pool.py", line 768, in get
raise self._value
Exception: bla 0
If I remove the offending process, everything works fine: (i.e. run with range(1,5)
)