在多进程中运行绘图时发生matplotlib错误 [英] matplotlib error when running plotting in multiprocess

查看:189
本文介绍了在多进程中运行绘图时发生matplotlib错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python的Multiprocess.Pool通过多个过程来绘制一些数据,如下所示:

I am using python's Multiprocess.Pool to plot some data using multiple processes as follows:

class plotDriver:
    def plot(self, parameterList):
        numberOfWorkers = len(parameterList)
        pool = Pool(numberOfWorkers)
        pool.map(plotWorkerFunction, parameterList)
        pool.close()
        pool.join()

这是我的课程的简化版本,驱动程序还包含我选择省略的其他内容. plotWorkderFunction 是一个单线程函数,该函数导入matplotlib并执行所有绘图和设置图形样式,并将绘图保存到一个pdf文件中,而每个工作人员都不会与另一个工作人员进行交互.

this is a simplified version of my class, the driver also contains other stuffs I choose to omit. The plotWorkderFunction is a single threaded function, which imports matplotlib and does all the plotting and setting figure styles and save the plots to one pdf file, and each worker is not interacting with the other.

我需要多次调用这个绘图函数,因为我有很多参数列表,如下所示:

I need to call this plot function multiple times since I have many parameterList, like following:

parameters = [parameterList0, parameterList1, ... parameterListn]
for param in parameters:
    driver = PlotDriver()
    driver.plot(param)

如果 parameters 只包含一个 parameterList(for 循环只运行一次),代码看起来工作正常.但是,只要 parameters 包含多个元素,它就会始终失败,并在循环中第二次出现以下错误消息.

If parameters only contains one parameterList (the for loop only runs once), the code seems working fine. But it consistently fails whenever parameters contains more than one element, with the following error message happening on the second time in the loop.

Traceback (most recent call last):
File "plot.py", line 59, in <module>
  plottingDriver.plot(outputFile_handle)
File "/home/yingryic/PlotDriver.py", line 69, in plot
  pool.map(plotWrapper, workerParamList)
File "/home/yingryic/.conda/envs/pp/lib/python2.7/multiprocessing/pool.py", line 251, in map
  return self.map_async(func.iterable, chunksize).get()
File "/home/yingryic/.conda/envs/pp/python2.7/multiprocessing/pool.py", line 567, in get
  raise self._value
RuntimeError: In set_text: could not load glyph
X Error: BadIDChoice (invalid resouce ID chosen for this connection) 14
  Extension: 138 (RENDER)
  Minor opcode: 17 (RenderCreateGlyphSet)
  Resouce id: 0xe00002
 : Fatal IO error: client killed

知道发生了什么问题以及如何解决?

any idea what is going wrong and how should I fix?

推荐答案

您可以尝试将 import matplotlib 放入 plotWorkerFunction() 中,以便子进程拥有自己的副本模块的

You can try placing import matplotlib into plotWorkerFunction() so that child processes will have their own copy of the module.

这篇关于在多进程中运行绘图时发生matplotlib错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆