解決python多線程報錯:AttributeError: Can't pickle local object問題
報錯信息:
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
原類的構造函數(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中對屬性進行初始化使用了其它類返回的句柄進行初始化導致,HTTPDigestAuth的返回值不能進行序列化,也就是不能作為cls(buf, protocol).dump(obj)的參數(shù)進行序列化。
將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多線程報錯:AttributeError: Can't pickle local object問題就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
使用coverage統(tǒng)計python web項目代碼覆蓋率的方法詳解
這篇文章主要介紹了使用coverage統(tǒng)計python web項目代碼覆蓋率的方法,詳細分析了coverage的安裝以及coverage命令統(tǒng)計py文件相關操作技巧,需要的朋友可以參考下2019-08-08Python?OpenCV實現(xiàn)圖片預處理的方法詳解
這篇文章主要為大家詳細介紹了Python?OpenCV實現(xiàn)圖片預處理的方法,文中的示例代碼講解詳細,具有一定的借鑒價值,感興趣的可以了解一下2022-09-09解決python3.6 右鍵沒有 Edit with IDLE的問題
這篇文章主要介紹了解決python3.6 右鍵沒有 Edit with IDLE的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03使用Python實現(xiàn)XLS和XLSX之間的相互轉換
在日常工作中,我們經(jīng)常需要處理和轉換不同格式的Excel文件,以適應不同的需求和軟件兼容性,Excel文件的兩種常見格式是XLS(Excel 97-2003)和XLSX(Excel 2007及以上版本),本文將詳細介紹如何使用Python在XLS和XLSX格式之間進行轉換,需要的朋友可以參考下2024-09-09簡單理解Python中的事件循環(huán)EventLoop
在 python 3中,加入了 asyncio 模塊,來實現(xiàn)協(xié)程,其中一個很重要的概念是事件循環(huán),本文我們就來自己實現(xiàn)一個相對簡單的EventLoop,從而了解一下事件循環(huán)是如何進行運轉的吧2023-10-10Python3使用騰訊云文字識別(騰訊OCR)提取圖片中的文字內容實例詳解
這篇文章主要介紹了Python3使用騰訊云文字識別(騰訊OCR)提取圖片中的文字內容方法詳解,需要的朋友可以參考下2020-02-02