Python multiprocess pool模塊報(bào)錯(cuò)pickling error問(wèn)題解決方法分析
本文實(shí)例講述了Python multiprocess pool模塊報(bào)錯(cuò)pickling error問(wèn)題解決方法。分享給大家供大家參考,具體如下:
問(wèn)題
之前在調(diào)用class內(nèi)的函數(shù)用multiprocessing模塊的pool函數(shù)進(jìn)行多線程處理的時(shí)候報(bào)了以下下錯(cuò)誤信息:
PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed
查了下官方文檔發(fā)現(xiàn)python默認(rèn)只能pickle以下的類(lèi)型:
- None, True, and False
- integers, floating point numbers, complex numbers
- strings, bytes, bytearrays
- tuples, lists, sets, and dictionaries containing only picklable objects
- functions defined at the top level of a module (using def, not lambda)
- built-in functions defined at the top level of a module
- classes that are defined at the top level of a module
- instances of such classes whose dict or the result of calling getstate() is picklable (see section -
- Pickling Class Instances for details).
函數(shù)只能pickle在頂層定義的函數(shù),很明顯的class內(nèi)的函數(shù)無(wú)法被pickle因此會(huì)報(bào)錯(cuò)。
import multiprocessing def work(): # top-level 函數(shù) print "work!" class Foo(): def work(self): # 非top-level函數(shù) print "work" pool1 = multiprocessing.Pool(processes=4) foo = Foo() pool1.apply_async(foo.work) pool1.close() pool1.join() # 此時(shí)報(bào)錯(cuò) pool2 = multiprocessing.Pool(processes=4) pool2.apply_async(work) pool2.close() pool2.join() # 此時(shí)工作正常
解決方案
調(diào)用pathos包下的multiprocessing模塊代替原生的multiprocessing。pathos中multiprocessing是用dill包改寫(xiě)過(guò)的,dill包可以將幾乎所有python的類(lèi)型都serialize,因此都可以被pickle。或者也可以自己用dill寫(xiě)一個(gè)(有點(diǎn)重復(fù)造輪子之嫌?。?/p>
參考
1. https://stackoverflow.com/questions/8804830/python-multiprocessing-picklingerror-cant-pickle-type-function
2. https://docs.python.org/3/library/pickle.html#what-can-be-pickled-and-unpickled
3. https://github.com/uqfoundation/pathos
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Python進(jìn)程與線程操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門(mén)與進(jìn)階經(jīng)典教程》、《Python+MySQL數(shù)據(jù)庫(kù)程序設(shè)計(jì)入門(mén)教程》及《Python常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
將pytorch的網(wǎng)絡(luò)等轉(zhuǎn)移到cuda
這篇文章主要介紹了將pytorch的網(wǎng)絡(luò)等轉(zhuǎn)移到cuda的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06詳解opencv中畫(huà)圓circle函數(shù)和橢圓ellipse函數(shù)
這篇文章主要介紹了opencv中畫(huà)圓circle函數(shù)和橢圓ellipse函數(shù),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12python爬蟲(chóng)解決驗(yàn)證碼的思路及示例
這篇文章主要介紹了python爬蟲(chóng)解決驗(yàn)證碼的思路及示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08一文總結(jié)學(xué)習(xí)Python的14張思維導(dǎo)圖
一文總結(jié)學(xué)習(xí)Python的14張思維導(dǎo)圖,本文涵蓋了Python編程的核心知識(shí),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10python3實(shí)現(xiàn)Dijkstra算法最短路徑的實(shí)現(xiàn)
這篇文章主要介紹了python3實(shí)現(xiàn)Dijkstra算法最短路徑的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05python用moviepy對(duì)視頻進(jìn)行簡(jiǎn)單的處理
這篇文章主要介紹了python如何用moviepy對(duì)視頻進(jìn)行簡(jiǎn)單的處理,幫助大家更好的利用python處理視頻,感興趣的朋友可以了解下2021-03-03