matplotlib error when running plotting in multiprocess
Asked Answered
P

1

9

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()

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)

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?

Paterfamilias answered 21/6, 2017 at 3:7 Comment(1)
I get exactly the same bug. My workaround was to catch this exception, and try a second time. Usually it works on the 2nd or 3rd attempt, maybe if different threads are trying to load glyph at the same time.Mis
N
1

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

Namesake answered 15/10, 2018 at 22:42 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.