解決python多線程報(bào)錯(cuò):AttributeError: Can't pickle local object問(wèn)題
報(bào)錯(cuò)信息:
Traceback (most recent call last):
File “D:/flaskProject/test.py”, line 35, in test
pool.apply(self.out, args=(i,))
File “Python37-32\lib\multiprocessing\pool.py", line 261, in apply
return self.apply_async(func, args, kwds).get()
File "\lib\multiprocessing\pool.py”, line 657, in get
raise self._value
File “\Python37-32\lib\multiprocessing\pool.py", line 431, in _handle_tasks
put(task)
File "\Python37-32\lib\multiprocessing\connection.py”, line 206, in send
self._send_bytes(_ForkingPickler.dumps(obj))
File “*\Python37-32\lib\multiprocessing\reduction.py”, line 51, in dumps
cls(buf, protocol).dump(obj)
TypeError: can't pickle _thread._local objects
原類的構(gòu)造函數(shù):
class threadtest: def __init__(self, ipList, user, password): self.ipList = ipList self.httpAuth = HTTPDigestAuth(user, password) return def out(self, i): url = "http://" + i + "/name" response = requests.get(url, self.httpAuth) print(response.text) return def test(self): pool = Pool(processes=2) for i in self.ipList: pool.apply(self.out, args=(i,)) pool.close() pool.join() return
if name == ‘main': ipList = [‘192.168.2.1', ‘192.168.2.2', ‘192.168.2.3', ‘192.168.2.4', ‘192.168.2.5', ] a = threadtest(ipList, ‘a(chǎn)dmin', ‘a(chǎn)dmin') a.test()
原因:
在class中對(duì)屬性進(jìn)行初始化使用了其它類返回的句柄進(jìn)行初始化導(dǎo)致,HTTPDigestAuth的返回值不能進(jìn)行序列化,也就是不能作為cls(buf, protocol).dump(obj)的參數(shù)進(jìn)行序列化。
將self.httpAuth = HTTPDigestAuth(httpUser, httpPassword)修改為:
self.httpUser
self.httpPassword
并將函數(shù)HTTPDigestAuth放到類的方法中
修改后:
class threadtest: def __init__(self, ipList, user, password): self.ipList = ipList self.user = user self.password = password return def out(self, i): url = "http://" + i + "/name" response = requests.get(url, HTTPDigestAuth(self.user, self.password)) print(response.text) return def test(self): pool = Pool(processes=2) for i in self.ipList: pool.apply(self.out, args=(i,)) pool.close() pool.join() return
if name == ‘main': ipList = [‘192.168.2.1', ‘192.168.2.2', ‘192.168.2.3', ‘192.168.2.4', ‘192.168.2.5', ] a = threadtest(ipList, ‘a(chǎn)dmin', ‘a(chǎn)dmin') a.test()
以上這篇解決python多線程報(bào)錯(cuò):AttributeError: Can't pickle local object問(wèn)題就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python編程實(shí)現(xiàn)的簡(jiǎn)單Web服務(wù)器示例
這篇文章主要介紹了Python編程實(shí)現(xiàn)的簡(jiǎn)單Web服務(wù)器功能,涉及Python URL請(qǐng)求與響應(yīng)相關(guān)操作技巧,需要的朋友可以參考下2017-06-06使用coverage統(tǒng)計(jì)python web項(xiàng)目代碼覆蓋率的方法詳解
這篇文章主要介紹了使用coverage統(tǒng)計(jì)python web項(xiàng)目代碼覆蓋率的方法,詳細(xì)分析了coverage的安裝以及coverage命令統(tǒng)計(jì)py文件相關(guān)操作技巧,需要的朋友可以參考下2019-08-08Python?OpenCV實(shí)現(xiàn)圖片預(yù)處理的方法詳解
這篇文章主要為大家詳細(xì)介紹了Python?OpenCV實(shí)現(xiàn)圖片預(yù)處理的方法,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的可以了解一下2022-09-09解決python3.6 右鍵沒(méi)有 Edit with IDLE的問(wèn)題
這篇文章主要介紹了解決python3.6 右鍵沒(méi)有 Edit with IDLE的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03使用Python實(shí)現(xiàn)XLS和XLSX之間的相互轉(zhuǎn)換
在日常工作中,我們經(jīng)常需要處理和轉(zhuǎn)換不同格式的Excel文件,以適應(yīng)不同的需求和軟件兼容性,Excel文件的兩種常見(jiàn)格式是XLS(Excel 97-2003)和XLSX(Excel 2007及以上版本),本文將詳細(xì)介紹如何使用Python在XLS和XLSX格式之間進(jìn)行轉(zhuǎn)換,需要的朋友可以參考下2024-09-09簡(jiǎn)單理解Python中的事件循環(huán)EventLoop
在 python 3中,加入了 asyncio 模塊,來(lái)實(shí)現(xiàn)協(xié)程,其中一個(gè)很重要的概念是事件循環(huán),本文我們就來(lái)自己實(shí)現(xiàn)一個(gè)相對(duì)簡(jiǎn)單的EventLoop,從而了解一下事件循環(huán)是如何進(jìn)行運(yùn)轉(zhuǎn)的吧2023-10-10Python3使用騰訊云文字識(shí)別(騰訊OCR)提取圖片中的文字內(nèi)容實(shí)例詳解
這篇文章主要介紹了Python3使用騰訊云文字識(shí)別(騰訊OCR)提取圖片中的文字內(nèi)容方法詳解,需要的朋友可以參考下2020-02-02Python列表嵌套常見(jiàn)坑點(diǎn)及解決方案
這篇文章主要介紹了Python列表嵌套常見(jiàn)坑點(diǎn)及解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09