我試圖保存許多matplotlib數字png磁盤文件,因爲savefig()速度慢,我嘗試通過使用多進程模塊來提高速度。如何多重處理將圖保存到Python中的文件?
這裏是我的代碼:(我的環境是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
是什麼意思?如何解決它?
意圖,我沒有任何這方面的經驗,但是從外觀上來看,'pool.apply_async()'將在內部酸洗你要發送它的任務,以便一個單獨的進程/線程可以取消它並使用它(在Python中'pickle'模塊提供了一種序列化和反序列化任意對象的方法)。看起來像matplotlib數字不能被醃 – entropy 2013-03-05 13:26:18