使用 Pool 绘图时进程崩溃

Process crashes when plotting using Pool

我正在使用 Linux 18.04.2 LTS 64 位机器。

并行化的函数进行绘图,使用 savefig 模块以 pdf 格式保存绘图。

目前,我正在 运行进行 100 次迭代。奇怪的是,第 0、1、2 或 3 次迭代生成了一个正确的 pdf 文件,而所有其他生成的 pdf 文件的大小为 0 字节。

在每次迭代中都会生成一条类似于此的消息:

Gdk-Message: 14:41:40.404: python2.7: Fatal IO error 11 (Resource temporarily unavailable) on X server :0.

任务完成时,进程冻结,我必须重新启动系统。当我注释掉绘图部分时,代码完成了工作,但合并绘图部分会导致上述错误消息。

这些是执行多处理的代码行。当在没有这些行的情况下调用该函数时,将生成一个正确的 pdf 文件(即代码为 运行 顺序时)

from multiprocessing import Pool
import time
if __name__ == "__main__":
    # Step 1: Init multiprocessing.Pool()
    pool = Pool()
    results = []
    # Step 2: `pool.apply` the `mcoverpc()`
    print('START')
    start_time = int(round(time.time()))
    data = list(range(100))
    result_objects = [pool.apply_async(function, args=(num,arg1,arg2,arg3)) for num in data]
    # Step 3: Don't forget to close
    result_num = [r.get()[0] for r in result_objects]
    result_pc = [r.get()[1] for r in result_objects]
    result_fit_details = [r.get()[2] for r in result_objects]
    result_redchi = [r.get()[3] for r in result_objects] 
    pool.close()
    pool.terminate()
    print('END', int(round(time.time()))-start_time)

我必须通过简单地将 matplotlib 后端 non-interactive 类型 changing 来解决这个问题。特别是,我使用 PDF 作为后端。