我正在尝试将许多 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
这是什么意思 ?如何解决?