Python | A process in the process pool was terminated abruptly while the future was running or pending

错误处理。

问题 / 错误

代码报错 A process in the process pool was terminated abruptly while the future was running or pending


原因

原因是在多进程(ProcessPoolExecutor)中,multiprocessing.Manager().dict()multiprocessing.Manager().list() 不能作为返回值,和 pool.submit ().result () 有冲突。

猜测和序列化有关,没有深究。


解决方法

本质上都是要保证在子程序 return 时,返回值是 dictlist

推荐方法一。

方法一 使用普通 dictlist

在子程序里没必要使用进程安全的 multiprocessing.Manager().dict()multiprocessing.Manager().list(),可以直接使用普通 dictlist

方法二 使用 copy

也可以在 return 之前,使用 copydeepcopy 转成普通 dictlist


总结

multiprocessing.Manager().dict()multiprocessing.Manager().list() 和 pool.submit ().result () 有冲突。

所以要保证在子程序 return 时,返回值是 dictlist