以下是最小可重现代码:
import multiprocessing
class E(Exception):
def __init__(self, a1, a2):
Exception.__init__(self, '{}{}'.format(a1, a2))
def f(_):
raise E(1, 2)
multiprocessing.Pool(1).map(f, (1,))
这会导致以下错误:
Exception in thread Thread-5:
Traceback (most recent call last):
File "/usr/lib/python3.7/threading.py", line 917, in _bootstrap_inner
self.run()
File "/usr/lib/python3.7/threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "/usr/lib/python3.7/multiprocessing/pool.py", line 496, in _handle_results
task = get()
File "/usr/lib/python3.7/multiprocessing/contrnection.py", line 251, in recv
return _ForkingPickler.loads(buf.getbuffer())
TypeError: __init__() missing 1 required positional argument: 'a2'
有没有办法解决这个问题?类似的问题在这里提到:https://bugs.python.org/issue39751
print
,它就能正常工作。因此问题可能出现在引发异常的过程中。也许与与主线程通信有关。 - Curtwagner1984raise
中,池工作进程目标函数也会捕获错误并尝试将其发送回主进程,但无法对E
实例进行反序列化。 - Aaron