0

我正在尝试将许多 matplotlib 图形保存到 png 磁盘文件,因为 savefig() 很慢,我尝试通过使用多进程模块来提高速度。

这是我的代码:(我的环境是 Windows XP + python_2.6.1 + Matplotlib_1.2.0 + multiprocessing_0.70a1)

import multiprocessing
from figure_creation_mudule import fig_list

def savefig_worker(fig, img_type, folder_path):
    file_name = fig.FM_figname 
    fig.savefig(folder_path+"\\"+file_name+"."+img_type, format=img_type)
    return None

if __name__ == '__main__':
    pool = multiprocessing.Pool()
    for fig in fig_list:
        pool.apply_async(savefig_worker, [fig, 'png', 'D:\\img_folder'])
    pool.close()
    pool.join()

并且fig_list是从其他模块导入的列表,包含 matplotlib 图形对象。

>>> fig_list
[<matplotlib.figure.Figure object at 0x0AAA1670>, <matplotlib.figure.Figure object at 0x0AD2B210>, <matplotlib.figure.Figure object at 0x0B277FD0>]

当我运行代码时,它遇到了问题:

Exception in thread Thread-2:
Traceback (most recent call last):
  File "D:\Python\lib\threading.py", line 522, in __bootstrap_inner
    self.run()
  File "D:\Python\lib\threading.py", line 477, in run
    self.__target(*self.__args, **self.__kwargs)
  File "D:\Python\lib\multiprocessing\pool.py", line 225, in _handle_tasks
    put(task)
PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed

这是什么意思 ?如何解决?

4

1 回答 1

1

我调查了这一点,确实Pool.apply_async()会在幕后腌制物体。要确认这一点,请在 REPL 中尝试:

>>> from multiprocessing import Pool
>>> def test(obj):
...   print obj
... 
>>> class A():
...   def __getstate__(self):
...     print "pickling"
...     return {}
... 
>>> pool = Pool()
>>> pool.apply_async(test, [A()])
<multiprocessing.pool.ApplyResult object at 0x10bbe82d0>
pickling

>>> <__main__.A instance at 0x10bbe83b0>

为避免这种情况,您需要使用其他东西multiprocessing.Pool来完成这项工作。multiprocessing.Process能行得通。但是,您应该注意不要产生太多进程,否则您会减慢速度而不是加快速度。

编辑:如果您打算使用multiprocessing.Pool 此问题/答案应该会有所帮助

于 2013-03-05T13:40:50.550 回答