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 时,返回值是 dict
或 list
。
推荐方法一。
方法一 使用普通 dict
和 list
在子程序里没必要使用进程安全的 multiprocessing.Manager().dict()
和 multiprocessing.Manager().list()
,可以直接使用普通 dict
和 list
。
方法二 使用 copy
也可以在 return 之前,使用 copy
或 deepcopy
转成普通 dict
和 list
。
总结
multiprocessing.Manager().dict()
、multiprocessing.Manager().list()
和 pool.submit ().result () 有冲突。
所以要保证在子程序 return 时,返回值是 dict
或 list
。
来做第一个留言的人吧!